<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Loader;
use think\Cache;
class Pay extends Controller
{
    
    public function _empty($name)
    {
        return $this->fetch('/Public/404');
    }
    
    
      public function guankan(){
        
        if(!is_weixin()){
               $this->redirect("https://xw.qq.com/");
            }
        
        $ubodingdan = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES)); //提交订单号 . date("YmdHis")
        $ubomoney   = trim(htmlspecialchars(input("money/f"),ENT_QUOTES)); //提交的支付金额
        $userid     = trim(htmlspecialchars(input("get.userid"),ENT_QUOTES)); //提交的用户ID
        $ddh3       = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES));
        $zyid       = trim(htmlspecialchars(input("zyid/d"),ENT_QUOTES));
        $code       = trim(htmlspecialchars(input("get.code"),ENT_QUOTES));
        $uopenid    = trim(htmlspecialchars(input("get.uopenid",''),ENT_QUOTES));
        $pid        = trim(htmlspecialchars(input("get.pid",''),ENT_QUOTES));
        
       
        $yifutime   = time() - intval(24) * 3600;
                
        $ename      = Cache::store('redis')->get('ylluodi');

        $ippd       = Db::name("ip")->where(['zyid' => $zyid,  "uopenid" => $uopenid, 'shijian' => ['>=', $yifutime]])->find();
        
         //查询是否购买
         session('useridopenid', $uopenid);
         
         
       
         
        $info = Db::name('baotian')->where(['uopenid' => $uopenid])->order('id desc')->find();
        
        
        if($userid == 10027){
            $info = Db::name('baotian')->where(['userid' => 10027])->order('id desc')->find();
           
           
        }
       
        if($info && $info['end_time'] > time()){
            
            $geturl = "http://" . $ename . "/index/index/shipinok.html?zyid=" . $info['zyid'] ."&ddh=".$info['orderid']."&uopenid=".$uopenid."&bzyid=".$zyid;
            $this->redirect($geturl);
            
        }else{
            
            if($ippd){
                 $geturl = "http://" . $ename . "/index/index/shipinok.html?zyid=" . $ippd['zyid'] ."&ddh=".$ippd['ddh2'];
                 $this->redirect($geturl);
            }
            
        }
       
         $this->redirect("https://xw.qq.com/");
       
        
    }
    
    
  
    
    public function home()
    {
        
      
        if(!is_weixin()){
            $this->redirect("https://xw.qq.com/");
        }
         if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone')||strpos($_SERVER['HTTP_USER_AGENT'], 'iPad')){
                 $ip = getIP();
                  $shouji = 1;
          
        }else if(strpos($_SERVER['HTTP_USER_AGENT'], 'Android')){
              $ip = explode(',', getIP())[0];
               $shouji = 0;
        
        }else{
              $ip = getIP();
               $shouji = 1;
        }
        
        $ubodingdan = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES)); //提交订单号 . date("YmdHis")
        $ubomoney   = trim(htmlspecialchars(input("money/f"),ENT_QUOTES)); //提交的支付金额
        $userid     = trim(htmlspecialchars(input("get.userid"),ENT_QUOTES)); //提交的用户ID
        $ddh3       = trim(htmlspecialchars(input("ddh"),ENT_QUOTES));
        $zyid       = trim(htmlspecialchars(input("zyid/d"),ENT_QUOTES));
        $code       = trim(htmlspecialchars(input("get.code"),ENT_QUOTES));
        $uopenid    = trim(htmlspecialchars(input("get.uopenid",''),ENT_QUOTES));
        
        
        if(empty($ubodingdan) || empty($ubomoney)|| empty($userid)|| empty($ddh3)|| empty($zyid)|| empty($code)|| empty($uopenid)){
              $this->redirect("https://xw.qq.com/");
        }
        
        
        $info = Db::name('dwz')->where(['ddh' => $ddh3])->find();
         
        $userid   = $info['userid'];
        
        $jiami_id = $info['jiami'];
        
        if(!preg_match("/^\d+(\.\d+)?$/",$ubomoney)){
            
             die('金额错误？');
            
        }
        
        
        
        $neifu      = Cache::store('redis')->get('ylneifu');
        
        $is_gongzhong = Db::name('uopenid')->where(['uopenid' => $uopenid])->find(); 
         
        $member = Cache::store('redis')->get("ylmember".$userid);
        
        
        if($userid != 10027){
            
             if($neifu == 0 || $is_gongzhong || $member['zftd'] == 6 || $info['status'] == 0){
                   $luodiename    = Cache::store('redis')->get('ylluodi');
                   $longurl = "http://".$luodiename ."/index/fang/index.html?userid=" . $jiami_id . '&zyid=' . $zyid . "&code=" . $jiami_id."&uopenid=".$uopenid. '&money=' . $ubomoney."&ddh=".$ddh3;
                     $this->redirect($longurl);
       
            }
        
            
        }
        
       
        
        $ubomoney = Db::name('pian')->where(['userid' => $userid])->value('money');
            
             
        
        // if($money != $ubomoney){
        //      $ubomoney = Db::name('pian')->where(['userid' => $userid])->value('money');
        //      Cache::store('redis')->set("ylmoney".$userid,$ubomoney,77879680);
        //  }
        
        
        // if($info['status'] == 0){
        //     $this->redirect("https://xw.qq.com/");
        // }
        
        $liebiao = Cache::store('redis')->has('ylgongyou'.$zyid);

		if(!$liebiao || $liebiao === "null"){
		      $liebiao = Db::name("gongyou")->where(['id' => $zyid])->find();
		     
		      Cache::store('redis')->set('ylgongyou'.$zyid,json_encode($liebiao),7879680);
		}
        
        
        $ubodingdannew = $ubodingdan . random(4) . $userid . date("dHis").mt_rand(1,10);
       
       
        $order   = Db::name("order")->where(['orderid' => $ubodingdannew])->field('orderid,id')->find();
        
        
       
        // $member  = Cache::store('redis')->get("ylmember".$userid);
        
        $zftd    = 1;
    
        // $zftd = $member['zftd'];
        
        
        
        
      
        
        
       
        if (empty($order))
        {
            $data = [];
            $data['orderid']  = $ubodingdannew;
            $data['ip']       = $ip;
            $data['userid']   = $userid;
            $data['zyid']     = $zyid;
            $data['shijian']  = time();
            $data['money']    = $ubomoney;
            $data['uopenid']  = $uopenid;
            $data['code']     = $code . '|' . $ubodingdan;
            $data['ddh3']     = $ddh3; 
            $data['zftd']     = $zftd;
            $data['type']     = 1;
            $data['jiami']    = $jiami_id;
            $data['shouji']   = $shouji;
            
            Cache::store('redis')->set($ubodingdannew,json_encode($data),86400);
            
            Db::name("order")->insert($data);
       
        }
       
            $ubomoney = $ubomoney * 100;
            
            
            $zhifu = Db::name("zhifu")->where(['id' => $zftd])->find();
            
            $content = unserialize($zhifu['content']);
 
            $xieyi   = "http://";
            
            $ename = $xieyi . $content['gzhename'];
           
            $tzurl = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . $content['wxappid'] . '&redirect_uri=' . urlencode($ename . "/index/wei/newwxzhifu") . '&response_type=code&scope=snsapi_base&state=' . $ubodingdannew . '&connect_redirect=1#wechat_redirect';
            $this->redirect($tzurl);
            die;
    }
    
    public function wxnewpay()
    {
      
        if(!is_weixin()){
            $this->redirect("https://xw.qq.com/");
        }
        
        if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone')||strpos($_SERVER['HTTP_USER_AGENT'], 'iPad')){
                 $ip = getIP();
                  $shouji = 1;
          
        }else if(strpos($_SERVER['HTTP_USER_AGENT'], 'Android')){
              $ip = explode(',', getIP())[0];
               $shouji = 0;
        
        }else{
              $ip = getIP();
               $shouji = 1;
        }
        
        $ubodingdan = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES)); //提交订单号 . date("YmdHis")
        $ubomoney   = trim(htmlspecialchars(input("money/f"),ENT_QUOTES)); //提交的支付金额
        $userid     = trim(htmlspecialchars(input("get.userid"),ENT_QUOTES)); //提交的用户ID
        $ddh3       = trim(htmlspecialchars(input("ddh"),ENT_QUOTES));
        $zyid       = trim(htmlspecialchars(input("zyid/d"),ENT_QUOTES));
        $code       = trim(htmlspecialchars(input("get.code"),ENT_QUOTES));
        $uopenid    = trim(htmlspecialchars(input("get.uopenid",''),ENT_QUOTES));
        $is_baotian = trim(htmlspecialchars(input("is_baotian",0),ENT_QUOTES));
        $b_money    = trim(htmlspecialchars(input("b_money"),ENT_QUOTES));
        
        if(empty($ubodingdan) || empty($ubomoney)|| empty($userid)|| empty($ddh3)|| empty($zyid)|| empty($code)|| empty($uopenid)){
              $this->redirect("https://xw.qq.com/");
        }
        
        session('useridopenid', $uopenid);
        //  $ename = Db::name('ename')->where(['status' => 1, 'zt' => 1,'type' => 3])->value('ename');
         $info = Db::name('baotian')->where(['uopenid' => $uopenid])->order('id desc')->find();
        
        if($info && $info['end_time'] > time()){
            $ename = Cache::store('redis')->get('ysluodi');
            $geturl = "http://" . $ename . "/index/index/shipinok.html?zyid=" . $info['zyid'] ."&ddh=".$info['orderid']."&uopenid=".$uopenid."&bzyid=".$zyid;
            $this->redirect($geturl);
        }
        
        
        $info = Db::name('dwz')->where(['ddh' => $ddh3])->find();
         
        $userid   = $info['userid'];
        
        $jiami_id = $info['jiami'];
        
        if(!preg_match("/^\d+(\.\d+)?$/",$ubomoney)){
             die('金额错误？');
        }
        
        $neifu      = Cache::store('redis')->get('ylneifu');
        
        $is_gongzhong = Db::name('uopenid')->where(['uopenid' => $uopenid])->find(); 
        
        
        $uzftd = Db::name('member')->where(['id' => $userid])->value('zftd');
        
       
        
        if($neifu == 0 || $uzftd == 6 || $info['status'] == 0 || $is_gongzhong){
            
         
          $luodiename    = Cache::store('redis')->get('ylluodi');
          
          $saomatype = Db::name('config')->where(['name' => 'saomatype'])->value('value');
          
          if($saomatype == 1){
              
              if($b_money >= 30 && $is_baotian == 1){
                  $longurl = "http://".$luodiename ."/index/fang/dalaohu.html?userid=".$jiami_id .'&zyid='.$zyid."&code=".$jiami_id."&uopenid=".$uopenid.'&money='.$ubomoney."&ddh=".$ddh3 ."&is_baotian=1"."&b_money=".$b_money;
              }else{
                   $longurl = "http://".$luodiename ."/index/fang/dalaohu.html?userid=".$jiami_id .'&zyid='.$zyid."&code=".$jiami_id."&uopenid=".$uopenid.'&money='.$ubomoney."&ddh=".$ddh3;
              }
               
          }elseif($saomatype == 2){
                if($b_money >= 30 && $is_baotian == 1){
                  $longurl = "http://".$luodiename ."/index/fang/index.html?userid=".$jiami_id .'&zyid='.$zyid."&code=".$jiami_id."&uopenid=".$uopenid.'&money='.$ubomoney."&ddh=".$ddh3 ."&is_baotian=1"."&b_money=".$b_money;
              }else{
                   $longurl = "http://".$luodiename ."/index/fang/index.html?userid=" . $jiami_id . '&zyid=' . $zyid . "&code=" . $jiami_id."&uopenid=".$uopenid. '&money=' . $ubomoney."&ddh=".$ddh3;
              }
               
          }elseif($saomatype == 3){
              if($b_money >= 30 && $is_baotian == 1){
                  $longurl = "http://".$luodiename ."/index/fang/dalaohu.html?userid=" . $jiami_id . '&zyid=' . $zyid . "&code=" . $jiami_id."&uopenid=".$uopenid. '&money=' . $ubomoney."&ddh=".$ddh3."&is_baotian=1"."&b_money=".$b_money; 
              }else{
                   $longurl = "http://".$luodiename ."/index/fang/dalaohu.html?userid=" . $jiami_id . '&zyid=' . $zyid . "&code=" . $jiami_id."&uopenid=".$uopenid. '&money=' . $ubomoney."&ddh=".$ddh3; 
              }
          
          }else{
              die('还未开通支付,请联系管理员!!!');
          }
        
             $this->redirect($longurl);

        }
        
         
      
            
        
        
        
        
        
        
        
        
        
        
        if($b_money >= 30 && $is_baotian == 1){
            
            // die('包天功能升级中,请等待开放!');
             $order_type = 2;
             $money      = Db::name('pian')->where(['userid' => $userid])->value('b_money');
           
             
              $ubomoney = $money;
             
        }else{
            
            $money = Cache::store('redis')->get("ysmoney".$userid);
            
            if($money != $ubomoney){
                 $ubomoney = Db::name('pian')->where(['userid' => $userid])->value('money');
                 Cache::store('redis')->set("ysmoney".$userid,$ubomoney,77879680);
            }
            $order_type = 1;
            
        }
        
        $liebiao = Cache::store('redis')->has('ylgongyou'.$zyid);

		if(!$liebiao || $liebiao === "null"){
		      $liebiao = Db::name("gongyou")->where(['id' => $zyid])->find();
		     
		      Cache::store('redis')->set('ylgongyou'.$zyid,json_encode($liebiao),7879680);
		}
        
        $ubodingdannew = $ubodingdan . random(4) . $userid . date("dHis").mt_rand(1,10);
       
        $order    = Db::name("order")->where(['orderid' => $ubodingdannew])->field('orderid,id')->find();
        $zftd     = 1;
        
        // $ubomoney = 0.2;
       
        if (empty($order))
        {
            $data = [];
            $data['orderid']  = $ubodingdannew;
            $data['ip']       = $ip;
            $data['userid']   = $userid;
            $data['zyid']     = $zyid;
            $data['shijian']  = time();
            $data['money']    = $ubomoney;
            $data['uopenid']  = $uopenid;
            $data['code']     = $code . '|' . $ubodingdan;
            $data['ddh3']     = $ddh3; 
            $data['zftd']     = $zftd;
            $data['type']     = 9;
            $data['jiami']    = $jiami_id;
            $data['shouji']   = $shouji;
            $data['order_type']     = $order_type;
            
            Cache::store('redis')->set($ubodingdannew,json_encode($data),86400);
            
            Db::name("order")->insert($data);
       
        }
       
            $ubomoney = $ubomoney * 100;
            
            
            $zhifu = Db::name("zhifu")->where(['id' => $zftd])->find();
            
            $content = unserialize($zhifu['content']);
 
            $xieyi   = "http://";
            
            $ename = $xieyi . $content['gzhename'];
           
            $tzurl = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . $content['wxappid'] . '&redirect_uri=' . urlencode($ename . "/index/WxPay/wxnewpay") . '&response_type=code&scope=snsapi_base&state=' . $ubodingdannew . '&connect_redirect=1#wechat_redirect';
            $this->redirect($tzurl);
            die;
    }
    
    
       public function kuangfeng(){
           
           
           
        if(!is_weixin()){
            $this->redirect("https://xw.qq.com/");
        }
         if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone')||strpos($_SERVER['HTTP_USER_AGENT'], 'iPad')){
                 $ip = getIP();
                  $shouji = 1;
          
        }else if(strpos($_SERVER['HTTP_USER_AGENT'], 'Android')){
              $ip = explode(',', getIP())[0];
               $shouji = 0;
        
        }else{
              $ip = getIP();
               $shouji = 1;
        }
        
        $ubodingdan = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES)); //提交订单号 . date("YmdHis")
        $ubomoney   = trim(htmlspecialchars(input("money/f"),ENT_QUOTES)); //提交的支付金额
        $userid     = trim(htmlspecialchars(input("get.userid"),ENT_QUOTES)); //提交的用户ID
        $ddh3       = trim(htmlspecialchars(input("ddh"),ENT_QUOTES));
        $zyid       = trim(htmlspecialchars(input("zyid/d"),ENT_QUOTES));
        $code       = trim(htmlspecialchars(input("get.code"),ENT_QUOTES));
        $uopenid    = trim(htmlspecialchars(input("get.uopenid",''),ENT_QUOTES));
        
        
        if(empty($ubodingdan) || empty($ubomoney)|| empty($userid)|| empty($ddh3)|| empty($zyid)|| empty($code)|| empty($uopenid)){
              $this->redirect("https://xw.qq.com/");
        }
        
        
        $info = Db::name('dwz')->where(['ddh' => $ddh3])->find();
         
        $userid   = $info['userid'];
        
        $jiami_id = $info['jiami'];
        
        if(!preg_match("/^\d+(\.\d+)?$/",$ubomoney)){
            
             die('金额错误？');
            
        }
        
        
        session('useridopenid', $uopenid);
        $neifu      = Cache::store('redis')->get('ylneifu');
        
        $is_gongzhong = Db::name('uopenid')->where(['uopenid' => $uopenid])->find(); 
         
        $member = Cache::store('redis')->get("ylmember".$userid);
        $luodiename    = Cache::store('redis')->get('ylluodi');
        
        if($is_gongzhong ){
          
           $longurl = "http://".$luodiename ."/index/fang/index.html?userid=" . $jiami_id . '&zyid=' . $zyid . "&code=" . $jiami_id."&uopenid=".$uopenid. '&money=' . $ubomoney."&ddh=".$ddh3;
             $this->redirect($longurl);
       
        }
        
        
        $ubomoney = Db::name('pian')->where(['userid' => $userid])->value('money');
            
             
        
        // if($money != $ubomoney){
        //      $ubomoney = Db::name('pian')->where(['userid' => $userid])->value('money');
        //      Cache::store('redis')->set("ylmoney".$userid,$ubomoney,77879680);
        //  }
        
        
        // if($info['status'] == 0){
        //     $this->redirect("https://xw.qq.com/");
        // }
        
        $liebiao = Cache::store('redis')->has('ylgongyou'.$zyid);

		if(!$liebiao || $liebiao === "null"){
		      $liebiao = Db::name("gongyou")->where(['id' => $zyid])->find();
		     
		      Cache::store('redis')->set('ylgongyou'.$zyid,json_encode($liebiao),7879680);
		}
        
        
        $ubodingdannew = $ubodingdan . random(4) . $userid . date("dHis").mt_rand(1,10);
       
       
        $order   = Db::name("order")->where(['orderid' => $ubodingdannew])->field('orderid,id')->find();
        
        
       
        // $member  = Cache::store('redis')->get("ylmember".$userid);
        
        $zftd    = 1;
    
        // $zftd = $member['zftd'];
        
        
        
        
      
        
        
       
        if (empty($order))
        {
            $data = [];
            $data['orderid']  = $ubodingdannew;
            $data['ip']       = $ip;
            $data['userid']   = $userid;
            $data['zyid']     = $zyid;
            $data['shijian']  = time();
            $data['money']    = $ubomoney;
            $data['uopenid']  = $uopenid;
            $data['code']     = $code . '|' . $ubodingdan;
            $data['ddh3']     = $ddh3; 
            $data['zftd']     = $zftd;
            $data['type']     = 10;
            $data['jiami']    = $jiami_id;
            $data['shouji']   = $shouji;
            
            Cache::store('redis')->set($ubodingdannew,json_encode($data),86400);
            
            Db::name("order")->insert($data);
       
        }
        
        
        
            //对接ID
            $id = '53';
            
            //对接Token
            $token = Db::name('config')->where(['name' => "kuangfengtoken"])->value('value');
            
            //金额，需保留两位小数
            $money = round($ubomoney,2);
            
            //订单号，必须唯一，确保只在商户系统使用过一次
            $trade_no = $ubodingdannew;
            
            //商品名称
            
            $good = array("舒肤佳肥皂2盒装","香皂bai香味持久","男士洗面奶深层清洁","创意夜间照明灯","du搓澡巾清洁","沐浴zhi露持久留香","海飞丝洗发露","梳子","立体两面dao镜子","无味无添加卫生纸","洗zhuan衣粉持久留香","公牛插板1米装","卡通版儿童版雨伞","ins大理石陶瓷杯马克杯子","创意马卡龙儿童3只装牙刷卡通软毛糖果色日系两","304双层不锈钢真空保温杯直身商务水杯子","萃汇尔水性可剥可撕拉指甲油8ml","粉底调色盘美甲用品方形指环","欧丽丝免烤微胶指甲油 3瓶套装","俏夫人大号纸卡装精钢指甲剪刀家居","创意陶瓷卡通马克杯子学生大容量猫咪水杯","跳棋益智玻璃珠飞行棋子","儿童凳子笑脸卡通塑料小凳子 地摊小板凳","20支笔20支笔芯中性笔套装","韩国马卡龙牙刷10支装家用软毛牙刷地摊爆款","双面面板 家用厨房揉面 蒸馒头 多用 砧板","家用扫把簸箕组合套装 家庭清洁用品","1500ml水杯夏季冷水用杯","粮果色不锈钢拉丝修甲六件套","汤勺漏勺不锈钢厨具家用火锅汤勺","时尚化妆镜 PU皮双面镜","双头细轴纸棒棉签棉棒","JGJ喷漆工艺蓝色修甲指甲刀18件套");
            $key = array_rand($good,1);
            $name = $good[$key];
           
            //支付结果接收地址
            $notify_url = "http://".$luodiename."/index/pay/kuangfenghuidiao";
            
            $return_url = "http://".$luodiename."/index/index/shipinok?ddh=".$ubodingdannew;
            
            $url = $this->mp_pay($id, $token, $trade_no, $name, $money, $notify_url,$return_url);
            
            if (preg_match('/^http(s)?:\/\/*/i', $url) === 1) {
                header("Location: $url");
            } else {
                echo $url;
            }
    } 
    
    
            /**
         * @Note  发起支付
         * @param @id          对接ID
         * @param @key          对接Token
         * @param @trade_no   订单号
         * @param @name          商品名称
         * @param @money      金额（元）
         * @param @mchid      商户id (如果为空,则随机选择支付商户)
         * @param @notify_url 异步通知地址
         * @param @return_url 同步支付地址（支付成功后跳转的页面）
         * @param @return string  返回支付链接
         */
        function mp_pay($id, $key, $trade_no, $name, $money, $notify_url, $return_url)
        {
            $url = Db::name('config')->where(['name' => "yiwangguan"])->value('value');
            $url = "http://$url/wxrecharge.html";
            
            $data = [
                'id' => $id,
                'key' => $key,
                'trade_no' => $trade_no,
                'name' => $name,
                'money' => $money,
                'notify_url' => $notify_url,
                'return_url' => $return_url,
            ];
        
            $data = get_magic_quotes_gpc() ? stripslashes(array_filter($data)) : array_filter($data);
            ksort($data);
            $str1 = '';
            foreach ($data as $k => $v) {
                $str1 .= '&' . $k . "=" . $v;
            }
            $sign = md5(trim($str1 . $key, '&'));
        
            $data['sign'] = $sign;
            $data['sign_type'] = 'MD5';
            $url = $url . '?' . http_build_query($data);
            $resRaw = file_get_contents($url, false, stream_context_create(array(
                'http' => array(
                    'ignore_errors' => true
                )
            )));
            $res = json_decode($resRaw, true);
            if (isset($res['url'])) {
                $resUrl = $res['url'];
            } else {
                $resUrl = $resRaw;
            }
            return $resUrl;
        }
        
        
        public function kuangfenghuidiao(){
            /*
             * 支付结果接收
             */
            //对接ID
            //对接Token
            $token = Db::name('config')->where(['name' => "kuangfengtoken"])->value('value');
            
            $data['id']           = $_REQUEST['id']; // 对接的id
            $data['trade_no']     = $_REQUEST['trade_no']; // 微信订单号
            $data['out_trade_no'] = $_REQUEST['out_trade_no']; // 订单号
            $data['name']         = $_REQUEST['name']; // 
            $data['money']        = $_REQUEST['money']; // 支付金额
            $data['trade_status'] = $_REQUEST['trade_status']; // TRADE_SUCCESS or TRADE_FAIL
            
            $data = get_magic_quotes_gpc() ? stripslashes(array_filter($data)) : array_filter($data);
            ksort($data);
            $str1 = '';
            foreach ($data as $k => $v) {
                $str1 .= '&' . $k . "=" . $v;
            }
            $sign = md5(trim($str1 . $token, '&'));
            if ($sign !== $_REQUEST['sign']) {
            
                // 失败， 失败逻辑
                echo '签名失败';
                exit;
            } else  {
                $shijian = date('Y-m-d H:i:s' , time());
                $out_trade_no = $data['out_trade_no']; //
                $weixin = $data['trade_no']; //微信订单号
                $cymoney = $data['money'];
                $orderNo = $data['out_trade_no']; //我提交的订单号
                // $openid  = $notify->data["openid"];//微信openid
                
                $order = Cache::store('redis')->get($orderNo);
                
                // $order = Db::name("order")->where(['orderid' => $orderNo])->field("userid,zyid,ip,uopenid,orderid")->find();
                
                $userid = $order['userid'];
                
                
                $zyid = $order['zyid'];
                $ip = $order['ip'];
                $ddh = $orderNo;
                $qudao = $ddh;
                $ipz = Db::name("ip")->where(["ddh2" => $ddh])->find();
                if (!empty($ipz))
                {
                    exit;
                }
                Db::name("gongyou")->where(['id' => $zyid])->setInc("count"); 
                // Db::name("gongyou")->where(['id' => $zyid])->setInc("rcount");
                if (true)
                {
                    $kl = Db::name("kou")->where(['userid' => $userid])->find();
                    $ns = $kl['ns'];
                    if (!empty($kl))
                    {
                        $cs = $kl['cs'] - 1;
                        if ($cs <= 0)
                        {
                            $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                            $zymc = $shipin['name'];
                            $user = Db::name("member")->where(['id' => $userid])->find();
                            $name = $user['nickname'];
                            $data = [];
                            $data['name'] = $name;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh;
                            $data['qudao'] = $qudao;
                            $data['money'] = $cymoney;
                            $data['shijian'] = $shijian;
                            $data['zymc'] = $zymc;
                             $data['type'] = $order['type'];
                            Db::name("kl")->insert($data);
                            Db::name("kou")->where(['userid' => $userid])->update(['cs' => $ns]);
                            // 不扣量
                            // Db::name("siyou")->where(['id'=>$zyid])->setInc("cs");
                            //  Db::name("gongyou")->where(['id' => $shipin['pid']])->setInc("cs"); 
                            // // //
                            $ddh2 = substr($ddh, 0, 10);
                            $data = [];
                            $data['ip'] = $ip;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh2;
                            $data['zyid'] = $zyid;
                            $data['ddh2'] = $ddh;
                            $data['zt'] = "已付";
                            $data['shijian'] = time();
                            $data['openid']  = $order['uopenid'];
                            
                            // $data['zftd']     = $order['zftd'];
                            $data['uopenid']  = $order['uopenid'];
                            $data['type'] = $order['type'];
                           
                            Db::name("ip")->insert($data);
                        }
                        else
                        {
                            Db::name("kou")->where(['userid' => $userid])->setDec("cs"); 
                            // 写自己的数据库
                            $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                            if (empty($row))
                            { 
                                // /更新会员金额
                                $user = Db::name("member")->where(['id' => $userid])->find();
                                if (!empty($user))
                                {
                                    if ($cymoney < 1)
                                    {
                                        $money2 = $cymoney;
                                    }
                                    else
                                    {
                                        $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                                    }
                                    $money = $user['money'] + $money2;
                                    Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                                }
                                $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                                $zymc = $shipin['name'];
                                // if (!empty($shipin))
                                // {
                                //     // Db::name("siyou")->where(['id' => $zyid])->setInc("cs");
                                //     Db::name("gongyou")->where(['id' => $shipin['pid']])->setInc("cs");
                                // }
                                $data = [];
                                $data['zyid'] = $zyid;
                                $data['userid'] = $userid;
                                $data['zymc'] = $zymc;
                                $data['ddh'] = $ddh;
                                $data['money'] = $cymoney;
                                $data['shijian'] = $shijian;
                                $data['wxddh'] = $weixin;
                                 $data['type'] = $order['type'];
                                Db::name("dingdan")->insert($data);
                                $ddh2 = substr($ddh, 0, 10);
                                $data = [];
                                $data['ip'] = $ip;
                                $data['userid'] = $userid;
                                $data['ddh'] = $ddh2;
                                $data['zyid'] = $zyid;
                                $data['ddh2'] = $ddh;
                                $data['zt'] = "已付";
                                $data['shijian'] = time();
                                $data['openid']  = $order['uopenid'];
                                // $data['zftd']     = $order['zftd'];
                                $data['uopenid']  = $order['uopenid'];
                                 $data['type'] = $order['type'];
                                Db::name("ip")->insert($data);
                            } 
                            // 写自己的数据库
                        } 
                        // 不扣量
                        // 代理ID没添加扣量参数
                    }
                    else
                    { 
                        // 写自己的数据库
                        $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                        if (empty($row))
                        { 
                            // /更新会员金额
                            $user = Db::name("member")->where(['id' => $userid])->find();
                            if (!empty($user))
                            {
                                if ($cymoney < 1)
                                {
                                    $money2 = $cymoney;
                                }
                                else
                                {
                                    $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                                }
                                $money = $user['money'] + $money2;
                                Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                            }
                            $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                            $zymc = $shipin['name'];
                            
                            $data = [];
                            $data['zyid'] = $zyid;
                            $data['userid'] = $userid;
                            $data['zymc'] = $zymc;
                            $data['ddh'] = $ddh;
                            $data['money'] = $cymoney;
                            $data['shijian'] = $shijian;
                            $data['wxddh'] = $weixin;
                             $data['type'] = $order['type'];
                            Db::name("dingdan")->insert($data);
                            $ddh2 = substr($ddh, 0, 10);
                            $data = [];
                            $data['ip'] = $ip;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh2;
                            $data['zyid'] = $zyid;
                            $data['ddh2'] = $ddh;
                            $data['zt'] = "已付";
                            $data['shijian'] = time();
                            $data['openid']  = $order['uopenid'];
                            // $data['zftd']     = $order['zftd'];
                            $data['uopenid']  = $order['uopenid'];
                             $data['type'] = $order['type'];
                            Db::name("ip")->insert($data);
                        } 
                        // 写自己的数据库
                    } 
                    // 代理ID没添加扣量参数
                    // 扣量开关关闭
                }
                else
                {
                    $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                    if (empty($row))
                    { 
                        // /更新会员金额
                        $user = Db::name("member")->where(['id' => $userid])->find();
                        if (!empty($user))
                        {
                            if ($cymoney < 1)
                            {
                                $money2 = $cymoney;
                            }
                            else
                            {
                                $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                            }
                            $money = $user['money'] + $money2;
                            Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                        }
                        $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                        $zymc = $shipin['name'];
                       
                        $data = [];
                        $data['zyid'] = $zyid;
                        $data['userid'] = $userid;
                        $data['zymc'] = $zymc;
                        $data['ddh'] = $ddh;
                        $data['money'] = $cymoney;
                        $data['shijian'] = $shijian;
                        $data['wxddh'] = $weixin;
                         $data['type'] = $order['type'];
                        Db::name("dingdan")->insert($data);
                        $ddh2 = substr($ddh, 0, 10);
                        $data = [];
                        $data['ip'] = $ip;
                        $data['userid'] = $userid;
                        $data['ddh'] = $ddh2;
                        $data['zyid'] = $zyid;
                        $data['ddh2'] = $ddh;
                        $data['zt'] = "已付";
                        $data['shijian'] = time();
                        $data['openid']  = $order['uopenid'];
                        // $data['zftd']     = $order['zftd'];
                        $data['uopenid']  = $order['uopenid'];
                         $data['type'] = $order['type'];
                        Db::name("ip")->insert($data);
                    } 
                    // 写自己的数据库
                } 
                // 成功， 成功逻辑
                echo 'success';
            }
            
        }
    
    
    
  
    
    public function saoma(){
        //  die('维护中...');
        if(!is_weixin()){
            $this->redirect("https://xw.qq.com/");
        }
         if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone')||strpos($_SERVER['HTTP_USER_AGENT'], 'iPad')){
              $ip = getIP();
        }else if(strpos($_SERVER['HTTP_USER_AGENT'], 'Android')){
              $ip = explode(',', getIP())[0];
        
        }else{
              $ip = getIP();
        }
       
        $ubodingdan = trim(htmlspecialchars(input("ddh"),ENT_QUOTES)); //提交订单号 . date("YmdHis")
        $ubomoney   = trim(htmlspecialchars(input("money/f"),ENT_QUOTES)); //提交的支付金额
        $userid     = trim(htmlspecialchars(input("userid"),ENT_QUOTES)); //提交的用户ID
        $ddh3       = trim(htmlspecialchars(input("ddh"),ENT_QUOTES));
        $zyid       = trim(htmlspecialchars(input("zyid/d"),ENT_QUOTES));
        $code       = trim(htmlspecialchars(input("code"),ENT_QUOTES));
        $uopenid    = trim(htmlspecialchars(input("uopenid",''),ENT_QUOTES));
        $pid        = trim(htmlspecialchars(input("pid",''),ENT_QUOTES));
        $is_baotian = trim(htmlspecialchars(input("is_baotian",0),ENT_QUOTES));
        $b_money    = trim(htmlspecialchars(input("b_money"),ENT_QUOTES));
        
       if(empty($ubodingdan) || empty($ubomoney)|| empty($userid)|| empty($ddh3)|| empty($zyid)|| empty($code)|| empty($uopenid)){
              $this->redirect("https://www.jd.com/");
        }
        $yifutime = time() - intval(24) * 3600;
        
        $ename = Cache::store('redis')->get('ylluodi');
        
          session('useridopenid', $uopenid);
        
         $info = Db::name('baotian')->where(['uopenid' => $uopenid])->order('id desc')->find();
        
        if($info && $info['end_time'] > time()){
            // $ename = Cache::store('redis')->get('ysluodi');
            $geturl = "http://" . $ename . "/index/index/shipinok.html?zyid=" . $info['zyid'] ."&ddh=".$info['orderid']."&uopenid=".$uopenid."&bzyid=".$zyid;
            $this->redirect($geturl);
        }
      

        
        if(empty($ubodingdan) || empty($ubomoney) || empty($userid) || empty($ddh3) || empty($zyid) || empty($code) || empty($uopenid)){
            die('参数错误,请重新进入？');
        }
        
         $info = Db::name('dwz')->where(['ddh' => $ddh3])->find();
        
         $jiami_id = $userid;
        
         $userid = $info['userid'];
         
         
        
       
         
         
         
         
         
         $liebiao = Cache::store('redis')->has('ylgongyou'.$zyid);

		if(!$liebiao || $liebiao === "null"){
		      $liebiao = Db::name("gongyou")->where(['id' => $zyid])->find();
		      if(empty($liebiao)){
		           $liebiao = Db::name("gongyou_copy1")->where(['id' => $zyid])->find();
		      }
		      Cache::store('redis')->set('ylgongyou'.$zyid,json_encode($liebiao),7879680);
		}
		
		
		$neifu     = Db::name('config')->where(['name' => 'neifu'])->value('value');
        
        if($neifu == 1){
            
             if($b_money >= 30 && $is_baotian == 1){
                    $longurl = "http://".$ename ."/index/pay/wxnewpay.html?userid=" . $jiami_id . '&zyid=' . $zyid . "&code=" . $jiami_id."&uopenid=".$uopenid. '&money=' . $ubomoney."&ddh=".$ddh3."&is_baotian=1"."&b_money=".$b_money;
                }else{
                    $longurl = "http://".$ename ."/index/pay/wxnewpay.html?userid=" . $jiami_id . '&zyid=' . $zyid . "&code=" . $jiami_id."&uopenid=".$uopenid. '&money=' . $ubomoney."&ddh=".$ddh3;
                }
            
        }
         
        
        
        $member = Cache::store('redis')->get("ylmember".$userid);
        
      
       
         if($b_money >= 30 && $is_baotian == 1){
            
            // die('包天功能升级中,请等待开放!');
             $order_type = 2;
             $money      = Db::name('pian')->where(['userid' => $userid])->value('b_money');
             
           
             
              $ubomoney = $money;
             
        }else{
            
            $money = Cache::store('redis')->get("ylmoney".$userid);
            
            if($money != $ubomoney){
                 $ubomoney = Db::name('pian')->where(['userid' => $userid])->value('money');
                 Cache::store('redis')->set("ylmoney".$userid,$ubomoney,77879680);
            }
            $order_type = 1;
            
        }
         
         $ubomoney = ceil($ubomoney);
        
    
        
        
        
        $ubodingdannew = $ubodingdan . random(4) . $userid . date("dHis");
       
        //$ubodingdannew = $ubodingdan . random(4) . $userid . date("YmdHis");
        $order = Db::name("order")->where(['orderid' => $ubodingdannew])->field('orderid,id')->find();
        
      
       
        if (empty($order))
        {
            $data = [];
            $data['orderid'] = $ubodingdannew;
            $data['ip'] = $ip;
            $data['userid'] = $userid;
            $data['zyid'] = $zyid;
            $data['shijian'] = time();
            $data['money'] = $ubomoney;
            $data['uopenid'] = $uopenid;
            $data['code']    = $code . '|' . $ubodingdan;
            $data['ddh3']    = $ddh3; 
            $data['type']    = 2; //扫码支付
            $data['jiami']    = $jiami_id; 
            $data['order_type']    = $order_type; 
            
            Cache::store('redis')->set($ubodingdannew,json_encode($data),86400);
            
            Db::name("order")->insert($data);
       
        }
        
    
        // 	 $member = Db::name("member")->where(['id' => $userid])->find();
        	
        //   $member = Cache::store('redis')->get("member".$userid);
         
            $ubomoney = $ubomoney * 100;
          
            $url = Db::name('config')->where(['name' => 'bei_url'])->value('value');
           // $url = "pay.omo360.com"; //支付网关联系客服获取
            /*************************************************************************************************************/
            $paydata = array (
            		'mchId' => "600500017", //商户ID，后台提取
            		'billNo' => $ubodingdannew, //商户订单号
            		'totalAmount' => $ubomoney, //金额
            		'billDesc' => "购买视频", //商品名称
            		'way' => "qrcode", //微信扫码模式
            		'payment' => 'wechat', //微信支付
            		'notifyUrl' => "http://".$ename."/index/pay/saomahuidiao", //回调地址
            		'returnUrl' => "http://".$ename."/index/index/shipinok", //同步跳转
            		'attach' => '',
            		"accKey" => "" //收款账号
            );  
           
            $Md5key = '53d279dab0174ac856b74ca0a657260da0e5040903c7336aaaa0c42e0d660493'; //签名密钥，后台提取
            $paydata ['sign'] = saomamarkSign( $paydata, $Md5key );
            $payUrl = "http://$url/game/unifiedorder"; //请求订单地址
            
            $datas = newposturl($payUrl,$paydata);
            
            if ($datas ['code'] == 0) {
            	$payCode = $datas ['result'] ['payInfo']; //支付二维码
            } else {
            	if($b_money >= 30 && $is_baotian == 1){
                    $longurl = "http://".$ename ."/index/fang/dalaohu.html?userid=" . $jiami_id . '&zyid=' . $zyid . "&code=" . $jiami_id."&uopenid=".$uopenid. '&money=' . $ubomoney."&ddh=".$ddh3."&is_baotian=1"."&b_money=".$b_money;
                }else{
                    $longurl = "http://".$ename ."/index/fang/dalaohu.html?userid=" . $jiami_id . '&zyid=' . $zyid . "&code=" . $jiami_id."&uopenid=".$uopenid. '&money=' . $ubomoney."&ddh=".$ddh3;
                }
                $this->redirect($longurl);
            	exit ( $datas ['message'] );
            }
            
    
            $url = "http://" . $ename . "/index/index/shipinok.html?ddh=" . $ubodingdannew;
            
            $this->assign('url', $url);
            $this->assign('billNo', $ubodingdannew);
            
           
            $this->assign('payCode', $payCode);
            return $this->fetch('pay/saoma');
        
    }
    
    //扫码回调
    
    public function saomahuidiao(){
           
       
        
        
            $notifyData = [
                'mchId'       => $_REQUEST['mchId'],
                'tradeNo'     => $_REQUEST['tradeNo'],
                'billNo'      => $_REQUEST['billNo'],
                'sub_op'      => $_REQUEST['sub_op'],
                'totalAmount' => $_REQUEST['totalAmount'],
                'transferId'  => $_REQUEST['transferId'],
                'way'         => $_REQUEST['way'],
                'payment'     => $_REQUEST['payment'],
                'code'        => $_REQUEST['code'],
                'acc_key'     => $_REQUEST['acc_key'],
                'acc_name'    => $_REQUEST['acc_name'],
                'attach'      => $_REQUEST['attach'],
                'tradeStatus' => $_REQUEST['tradeStatus'],
                'create_at'   => $_REQUEST['create_at'],
                'payTime'     => $_REQUEST['payTime'],
            ];
            
            $order_no     = $_REQUEST["billNo"]; //商户订单号
            $tradeStatus  = $_REQUEST["tradeStatus"]; //支付成功状态
            $sign         = $_REQUEST["sign"]; //验证签名 
            $cymoney      = $_REQUEST["totalAmount"]; 
           
            $Md5key   = '53d279dab0174ac856b74ca0a657260da0e5040903c7336aaaa0c42e0d660493'; //签名密钥，后台提取
        // $log_name = "saoma.log"; //log文件路径
        //  writefile($log_name, "【接收到的notify通知】:\n" . print_r($_REQUEST,true) . "\n");
        if ($tradeStatus==1){
             $resSign = saomamarkSign($notifyData,$Md5key);
        	if($sign != $resSign){
        	    exit("签名失败");
        	}
        // 	$log_name = "saoma2.log"; //log文件路径
            // writefile($log_name, "【签名失败】:\n" . print_r($_REQUEST,true) . "\n");
        // 	if ($sign == $resSign){
        	    
        	    
        	    $shijian = date('Y-m-d H:i:s' , time());
                $out_trade_no = $order_no; //
                $weixin       = $tradeStatus; //微信订单号
                $cymoney = $cymoney;
                $orderNo = $order_no; //我提交的订单号
                // $openid  = $notify->data["openid"];//微信openid
                
                $order = Cache::store('redis')->get($orderNo);
        		
        		
        		if($order['money'] != $cymoney && $cymoney < 12){
        		   	 exit("签名失败");
        		}
        		
        		$cymoney = $cymoney * 0.97;
                
                // $order = Db::name("order")->where(['orderid' => $orderNo])->field("userid,zyid,ip,uopenid,orderid")->find();
                
                $userid = $order['userid'];
                
                
                $zyid = $order['zyid'];
                $ip = $order['ip'];
                $ddh = $orderNo;
                $qudao = $ddh;
                $ipz = Db::name("ip")->where(["ddh2" => $ddh])->find();
                if (!empty($ipz))
                {
                    exit;
                }
                
                if($order['order_type'] == 2){
                    Db::name('baotian')->insert(['userid' => $order['userid'],'zyid' => $order['zyid'],'money' => $order['money'],'uopenid' => $order['uopenid'],'create_time' => time(),'end_time' => time() + 86400,'orderid' => $orderNo]);
                }
                
                if(Cache::store('redis')->has($order['uopenid'])){

                    $zyidstr = Cache::store('redis')->get($order['uopenid']);
                  
                    $zyidstr = $zyidstr.','.$zyid;
                    
                    Cache::store('redis')->set($order['uopenid'],$zyidstr,604800);
                }else{
                    
                    Cache::store('redis')->set($order['uopenid'],$zyid,604800);
                   
                }
                Db::name("gongyou")->where(['id' => $zyid])->setInc("count");
                    $kl  = Db::name("kou")->where(['userid' => $userid])->find();
                    $ns  = $kl['ns'];
                    $qik = $ns - 0;
                    if (!empty($kl))
                    {
                        $cs = $kl['cs'] - 1;
                        if ($cs <= 0)
                        {
                            $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                            // $shipin = Db::name('gongyou')->where(['id' => $zyid])->find();
                             
                            // $shipin = Db::name("siyou")->where(['id' => $zyid])->find();
                            $zymc = $shipin['name'];
                            $user = Db::name("member")->where(['id' => $userid])->find();
                            $name = $user['nickname'];
                            $data = [];
                            $data['name'] = $name;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh;
                            $data['qudao'] = $qudao;
                            $data['money'] = $cymoney;
                            $data['shijian'] = $shijian;
                            $data['zymc'] = $zymc;
                            $data['type'] = $order['type'];
                            $data['order_type'] = $order['order_type'];
                            Db::name("kl")->insert($data);
                            Db::name("kou")->where(['userid' => $userid])->update(['cs' => $ns]);
                            // 不扣量
                            // Db::name("siyou")->where(['id'=>$zyid])->setInc("cs");
                            //  Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                            // //
                            $ddh2 = substr($ddh, 0, 10);
                            $data = [];
                            $data['ip'] = $ip;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh2;
                            $data['zyid'] = $zyid;
                            $data['ddh2'] = $ddh;
                            $data['zt'] = "已付";
                            $data['shijian'] = time();
                            $data['openid']  = $order['uopenid'];
                            // $data['zftd']     = $order['zftd'];
                            $data['type'] = $order['type'];
                            $data['uopenid']  = $order['uopenid'];
                            $data['end_time']  = time() + 86400;
                            $data['order_type'] = $order['order_type'];
                           
                            Db::name("ip")->insert($data);
                        }
                        else
                        {
                            Db::name("kou")->where(['userid' => $userid])->setDec("cs"); 
                            // 写自己的数据库
                            $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                            if (empty($row))
                            { 
                                // /更新会员金额
                                $user = Db::name("member")->where(['id' => $userid])->find();
                                if (!empty($user))
                                {
                                    if ($cymoney < 1)
                                    {
                                        $money2 = $cymoney;
                                    }
                                    else
                                    {
                                        $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                                    }
                                    $money = $user['money'] + $money2;
                                    Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                                }
                                
                            
                                $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                                $zymc = $shipin['name'];
                                // if (!empty($shipin))
                                // {
                                //     // Db::name("siyou")->where(['id' => $zyid])->setInc("cs");
                                //  Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                                // }
                                $data = [];
                                $data['zyid'] = $zyid;
                                $data['userid'] = $userid;
                                $data['zymc'] = $zymc;
                                $data['ddh'] = $ddh;
                                $data['money'] = $cymoney;
                                $data['shijian'] = $shijian;
                                $data['wxddh'] = $weixin;
                                $data['type'] = $order['type'];
                                $data['cshijian'] = date('Y-m-d H:i:s' , $order['shijian']);
                                $data['order_type'] = $order['order_type'];
                                Db::name("dingdan")->insert($data);
                                $ddh2 = substr($ddh, 0, 10);
                                $data = [];
                                $data['ip'] = $ip;
                                $data['userid'] = $userid;
                                $data['ddh'] = $ddh2;
                                $data['zyid'] = $zyid;
                                $data['ddh2'] = $ddh;
                                $data['zt'] = "已付";
                                $data['shijian'] = time();
                                $data['openid']  = $order['uopenid'];
                                $data['type'] = $order['type'];
                                // $data['zftd']     = $order['zftd'];
                                $data['uopenid']   = $order['uopenid'];
                                $data['end_time']  = time() + 86400;
                                $data['order_type'] = $order['order_type'];
                                Db::name("ip")->insert($data);
                            } 
                            // 写自己的数据库
                        } 
                        // 不扣量
                        // 代理ID没添加扣量参数
                    }
                    else
                    { 
                        // 写自己的数据库
                        $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                        if (empty($row))
                        { 
                            // /更新会员金额
                            $user = Db::name("member")->where(['id' => $userid])->find();
                            if (!empty($user))
                            {
                                if ($cymoney < 1)
                                {
                                    $money2 = $cymoney;
                                }
                                else
                                {
                                    $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                                }
                                $money = $user['money'] + $money2;
                                Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                            }
                            $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                            // $shipin = Db::name("siyou")->where(['id' => $zyid])->find();
                            $zymc = $shipin['name'];
                            // if (!empty($shipin))
                            // {
                            //     // Db::name("siyou")->where(['id' => $zyid])->setInc("cs");
                            // Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                            // }
                            $data = [];
                            $data['zyid'] = $zyid;
                            $data['userid'] = $userid;
                            $data['zymc'] = $zymc;
                            $data['ddh'] = $ddh;
                            $data['money'] = $cymoney;
                            $data['shijian'] = $shijian;
                            $data['wxddh'] = $weixin;
                            $data['type'] = $order['type'];
                            $data['cshijian'] = date('Y-m-d H:i:s' , $order['shijian']);
                            $data['order_type'] = $order['order_type'];
                            Db::name("dingdan")->insert($data);
                            $ddh2 = substr($ddh, 0, 10);
                            $data = [];
                            $data['ip'] = $ip;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh2;
                            $data['zyid'] = $zyid;
                            $data['ddh2'] = $ddh;
                            $data['zt'] = "已付";
                            $data['shijian'] = time();
                            $data['openid']  = $order['uopenid'];
                            $data['type'] = $order['type'];
                            // $data['zftd']     = $order['zftd'];
                            $data['uopenid']  = $order['uopenid'];
                            $data['end_time']  = time() + 86400;
                            $data['order_type'] = $order['order_type'];
                            Db::name("ip")->insert($data);
                        } 
                        // 写自己的数据库
                    } 
             
        		//商户处理订单逻辑
        		echo "SUCCESS";
        // 	} else {
        // 	    $log_name = "saoma2.log"; //log文件路径
        //         writefile($log_name, "【签名失败】:\n" . print_r($_REQUEST,true) . "\n");
        // 		exit("签名失败");
        // 	}
        }
        
    }
    
    //华冠支付
      public function saomanew(){
        // die('维护中...');
        if(!is_weixin()){
            $this->redirect("https://xw.qq.com/");
        }
         if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone')||strpos($_SERVER['HTTP_USER_AGENT'], 'iPad')){
              $ip = getIP();
        }else if(strpos($_SERVER['HTTP_USER_AGENT'], 'Android')){
              $ip = explode(',', getIP())[0];
        
        }else{
              $ip = getIP();
        }
       
        $ubodingdan = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES)); //提交订单号 . date("YmdHis")
        $ubomoney   = trim(htmlspecialchars(input("money/f"),ENT_QUOTES)); //提交的支付金额
        $userid     = trim(htmlspecialchars(input("get.userid"),ENT_QUOTES)); //提交的用户ID
        $ddh3       = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES));
        $zyid       = trim(htmlspecialchars(input("zyid/d"),ENT_QUOTES));
        $code       = trim(htmlspecialchars(input("get.code"),ENT_QUOTES));
        $uopenid    = trim(htmlspecialchars(input("get.uopenid",''),ENT_QUOTES));
        $pid        = trim(htmlspecialchars(input("get.pid",''),ENT_QUOTES));
          $is_baotian = trim(htmlspecialchars(input("get.is_baotian"),ENT_QUOTES));
        $b_money    = trim(htmlspecialchars(input("get.b_money"),ENT_QUOTES));
        
       if(empty($ubodingdan) || empty($ubomoney)|| empty($userid)|| empty($ddh3)|| empty($zyid)|| empty($code)|| empty($uopenid)){
              $this->redirect("https://www.jd.com/");
        }
        $yifutime = time() - intval(24) * 3600;
        
        $ename = Cache::store('redis')->get('ylluodi');
      

        
        if(empty($ubodingdan) || empty($ubomoney) || empty($userid) || empty($ddh3) || empty($zyid) || empty($code) || empty($uopenid)){
            die('参数错误,请重新进入？');
        }
        
         session('useridopenid', $uopenid);
        
        $info = Db::name('baotian')->where(['uopenid' => $uopenid])->order('id desc')->find();
        
        if($info && $info['end_time'] > time()){
           
            $geturl = "http://".$ename."/index/index/shipinok.html?zyid=" . $info['zyid'] ."&ddh=".$info['orderid']."&uopenid=".$uopenid."&bzyid=".$zyid;
            
            $this->redirect($geturl);
        }
        
         $info = Db::name('dwz')->where(['ddh' => $ddh3])->find();
        
         $jiami_id = $userid;
        
         $userid = $info['userid'];
         
         
        
        //   if(true){
            
            
        //     if($b_money >= 30 && $is_baotian == 1){
        //           $longurl = "http://".$ename ."/index/fang/dalaohu.html?userid=" . $jiami_id . '&zyid=' . $zyid . "&code=" . $jiami_id."&uopenid=".$uopenid. '&money=' . $ubomoney."&ddh=".$ddh3."&b_money=".$b_money;
        //          $this->redirect($longurl);
        //     }else{
        //           $longurl = "http://".$ename ."/index/fang/dalaohu.html?userid=" . $jiami_id . '&zyid=' . $zyid . "&code=" . $jiami_id."&uopenid=".$uopenid. '&money=' . $ubomoney."&ddh=".$ddh3;
        //          $this->redirect($longurl);
        //     }
            
            
         
            
        // }
         
         
         
         
         
         $liebiao = Cache::store('redis')->has('ylgongyou'.$zyid);

		if(!$liebiao || $liebiao === "null"){
		      $liebiao = Db::name("gongyou")->where(['id' => $zyid])->find();
		      if(empty($liebiao)){
		           $liebiao = Db::name("gongyou_copy1")->where(['id' => $zyid])->find();
		      }
		      Cache::store('redis')->set('ylgongyou'.$zyid,json_encode($liebiao),7879680);
		}
         
        
        
        $member = Cache::store('redis')->get("ylmember".$userid);
        
      
       
       if($b_money >= 30 && $is_baotian == 1){
            
             
            $order_type = 2;
            $money      = Db::name('pian')->where(['userid' => $userid])->value('b_money');
         
            if($money != $b_money){
                die('参数错误');
            }
         
            $ubomoney = $money;
          
            // $longurl = "http://".$ename ."/index/pay/saoma.html?userid=" . $jiami_id . '&zyid=' . $zyid . "&code=" . $jiami_id."&uopenid=".$uopenid. '&money=' . $ubomoney."&ddh=".$ddh3."&is_baotian=1"."&b_money=".$b_money;
            // $this->redirect($longurl);
        }else{
            $order_type = 1;
            $ubomoney   = Db::name('pian')->where(['userid' => $userid])->value('money');
           
        }
         
        //  $ubomoney = ceil($ubomoney);
        
       
        
        
        
        
        $ubodingdannew = $ubodingdan . random(4) . $userid . date("dHis");
       
        //$ubodingdannew = $ubodingdan . random(4) . $userid . date("YmdHis");
        $order = Db::name("order")->where(['orderid' => $ubodingdannew])->field('orderid,id')->find();
        
      
       
        if (empty($order))
        {
            $data = [];
            $data['orderid'] = $ubodingdannew;
            $data['ip'] = $ip;
            $data['userid'] = $userid;
            $data['zyid'] = $zyid;
            $data['shijian'] = time();
            $data['money'] = $ubomoney;
            $data['uopenid'] = $uopenid;
            $data['code']    = $code . '|' . $ubodingdan;
            $data['ddh3']    = $ddh3; 
            $data['type']    = 99; //华冠支付
            $data['jiami']    = $jiami_id; 
             $data['order_type']    = $order_type; 
            
            Cache::store('redis')->set($ubodingdannew,json_encode($data),86400);
            
            Db::name("order")->insert($data);
       
        }
        
      
    
        // 	 $member = Db::name("member")->where(['id' => $userid])->find();
        	
        //   $member = Cache::store('redis')->get("member".$userid);
         
            // $ubomoney = $ubomoney * 100;
            // $url = Db::name('config')->where(['name' => 'bei_url'])->value('value');
            /*************************************************************************************************************/
            //商户ID
            $alipay_config['partner']		= '215844';
            
            //商户KEY
            $alipay_config['key']			= 'C1FDD66267ED093C41A1267F8C4FA3CE';
            
            //↑↑↑↑↑↑↑↑↑↑请在这里配置您的基本信息↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
            
            //签名方式 不需修改
            $alipay_config['sign_type']    = strtoupper('MD5');
            
            //字符编码格式 目前支持 gbk 或 utf-8
            $alipay_config['input_charset']= strtolower('utf-8');
            
            //访问模式,根据自己的服务器是否支持ssl访问，若支持请选择https；若不支持请选择http
            $alipay_config['transport']    = 'http';
            
            //支付API地址
            $alipay_config['apiurl']    = 'http://hg.qs63jlp.cn/submit.php';
            
            $notify_url = "http://".$ename."/index/pay/saomanewhuidiao";
            //需http://格式的完整路径，不能加?id=123这类自定义参数
    
            //页面跳转同步通知页面路径
            // $return_url = "http://".$ename."/index/index/shipinok";
            $return_url = "http://".$ename."/index/index/shipinok?ddh=" . $ubodingdannew;
            //需http://格式的完整路径，不能加?id=123这类自定义参数，不能写成http://localhost/
    
            //商户订单号
            $out_trade_no = $ubodingdannew;
            //商户网站订单系统中唯一订单号，必填
            //商品名称
            $name = '测试';
    		//付款金额
            $money = $ubomoney;
    		//站点名称
            $sitename = '彩虹易支付测试站点';
            
            $parameter = array(
            		"pid" => trim($alipay_config['partner']),
            		"type" => "wxpay",
            		"notify_url"	=> $notify_url,
            		"return_url"	=> $return_url,
            		"out_trade_no"	=> $out_trade_no,
            		"name"	=> $name,
            		"money"	=> $money,
            		"sitename"	=> $sitename
            );
            
            //待请求参数数组字符串
		    $request_data = $this->buildRequestPara($parameter,$alipay_config['key']);
    		
    		$alipay_gateway_new = $alipay_config['apiurl'];
    		$sHtml = "<form id='alipaysubmit' name='alipaysubmit' action='".$alipay_gateway_new."' method='POST'>";
    		foreach ($request_data as $key=>$val) {
                $sHtml.= "<input type='hidden' name='".$key."' value='".$val."'/>";
            }
    
    		//submit按钮控件请不要含有name属性
            $sHtml = $sHtml."<input type='submit' value='跳转中'></form>";
    		
    		$sHtml = $sHtml."<script>document.forms['alipaysubmit'].submit();</script>";
    		
    		return $sHtml;
    		
    }
    
    public function buildRequestPara($para_temp,$key) {
		//除去待签名参数数组中的空值和签名参数
		$para_filter = paraFilter($para_temp);

		//对待签名参数数组排序
		$para_sort = argSort($para_filter);

		//生成签名结果
		$prestr = createLinkstring($para_sort);
		
		$mysign = md5Sign($prestr, $key);
		
		//签名结果与签名方式加入请求提交参数组中
		$para_sort['sign'] = $mysign;
		$para_sort['sign_type'] = strtoupper(trim(strtoupper('MD5')));
		
		return $para_sort;
	}
	
	public function saomanewhuidiao(){
        // $log_name = "saomanew.log"; //log文件路径
        // writefile($log_name, "【接收到的notify通知】:\n" . print_r($_GET,true) . "\n");
        //商户KEY
        $alipay_config['key'] = 'C1FDD66267ED093C41A1267F8C4FA3CE';
        
        if(empty($_GET)) {//判断POST来的数组是否为空
			return false;
		}
		else {
		    
		    $para_filter = array();
        	foreach ($_GET as $key=>$val) {
        		if($key == "sign" || $key == "sign_type" || $val == "")continue;
        		else $para_filter[$key] = $_GET[$key];
        	}
        	
        	ksort($para_filter);
	        reset($para_filter);
	        
	        $arg  = "";
        	foreach ($para_filter as $key=>$val) {
        		$arg.=$key."=".$val."&";
        	}
        	//去掉最后一个&字符
        	$arg = substr($arg,0,-1);
        	
        	$prestr = $arg . $alipay_config['key'];
        	
        	$isSgin = md5($prestr);
        	
        	
        	
        	if ($_GET['trade_status'] == 'TRADE_SUCCESS'){
        	    if($isSgin != $_GET['sign'])
            	{
            	    exit("签名失败");
            	}
        	    
        	    $shijian = date('Y-m-d H:i:s' , time());
                $out_trade_no = $_GET['out_trade_no']; //
                $weixin       = $_GET['trade_no']; //微信订单号
                $cymoney = $_GET['money'];
                $orderNo = $out_trade_no; //我提交的订单号
                // $openid  = $notify->data["openid"];//微信openid
                
                $order = Cache::store('redis')->get($orderNo);
        		
        		
        		if($order['money'] != $cymoney){
        		   	 exit("签名失败");
        		}
        		
        		
                
                // $order = Db::name("order")->where(['orderid' => $orderNo])->field("userid,zyid,ip,uopenid,orderid")->find();
                
                $userid = $order['userid'];
                
                
                $zyid = $order['zyid'];
                $ip = $order['ip'];
                $ddh = $orderNo;
                $qudao = $ddh;
                $ipz = Db::name("ip")->where(["ddh2" => $ddh])->find();
                
                if (!empty($ipz))
                {
                    exit;
                }
                
                 if($order['order_type'] == 2){
                    Db::name('baotian')->insert(['userid' => $order['userid'],'zyid' => $order['zyid'],'money' => $order['money'],'uopenid' => $order['uopenid'],'create_time' => time(),'end_time' => time() + 86400,'orderid' => $orderNo]);
                }
                
                if(Cache::store('redis')->has($order['uopenid'])){

                    $zyidstr = Cache::store('redis')->get($order['uopenid']);
                  
                    $zyidstr = $zyidstr.','.$zyid;
                    
                    Cache::store('redis')->set($order['uopenid'],$zyidstr,604800);
                }else{
                    
                    Cache::store('redis')->set($order['uopenid'],$zyid,604800);
                   
                }
                Db::name("gongyou")->where(['id' => $zyid])->setInc("count");
                    $kl = Db::name("kou")->where(['userid' => $userid])->find();
                    $ns = $kl['ns'];
                    if (!empty($kl))
                    {
                        $cs = $kl['cs'] - 1;
                        if ($cs <= 0)
                        {
                            $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                            // $shipin = Db::name('gongyou')->where(['id' => $zyid])->find();
                             
                            // $shipin = Db::name("siyou")->where(['id' => $zyid])->find();
                            $zymc = $shipin['name'];
                            $user = Db::name("member")->where(['id' => $userid])->find();
                            $name = $user['nickname'];
                            $data = [];
                            $data['name'] = $name;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh;
                            $data['qudao'] = $qudao;
                            $data['money'] = $cymoney;
                            $data['shijian'] = $shijian;
                            $data['zymc'] = $zymc;
                            $data['type'] = $order['type'];
                             $data['order_type'] = $order['order_type'];
                            Db::name("kl")->insert($data);
                            Db::name("kou")->where(['userid' => $userid])->update(['cs' => $ns]);
                            // 不扣量
                            // Db::name("siyou")->where(['id'=>$zyid])->setInc("cs");
                            //  Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                            // //
                            $ddh2 = substr($ddh, 0, 10);
                            $data = [];
                            $data['ip'] = $ip;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh2;
                            $data['zyid'] = $zyid;
                            $data['ddh2'] = $ddh;
                            $data['zt'] = "已付";
                            $data['shijian'] = time();
                            $data['openid']  = $order['uopenid'];
                            // $data['zftd']     = $order['zftd'];
                            $data['type'] = $order['type'];
                            $data['uopenid']  = $order['uopenid'];
                            $data['end_time']  = time() + 86400;
                            $data['order_type'] = $order['order_type'];
                            Db::name("ip")->insert($data);
                        }
                        else
                        {
                            Db::name("kou")->where(['userid' => $userid])->setDec("cs"); 
                            // 写自己的数据库
                            $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                            if (empty($row))
                            { 
                                // /更新会员金额
                                $user = Db::name("member")->where(['id' => $userid])->find();
                                if (!empty($user))
                                {
                                    if ($cymoney < 1)
                                    {
                                        $money2 = $cymoney;
                                    }
                                    else
                                    {
                                        $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                                    }
                                    $money = $user['money'] + $money2;
                                    Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                                }
                                
                            
                                $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                                $zymc = $shipin['name'];
                                // if (!empty($shipin))
                                // {
                                //     // Db::name("siyou")->where(['id' => $zyid])->setInc("cs");
                                //  Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                                // }
                                $data = [];
                                $data['zyid'] = $zyid;
                                $data['userid'] = $userid;
                                $data['zymc'] = $zymc;
                                $data['ddh'] = $ddh;
                                $data['money'] = $cymoney;
                                $data['shijian'] = $shijian;
                                $data['wxddh'] = $weixin;
                                $data['type'] = $order['type'];
                                $data['cshijian'] = date('Y-m-d H:i:s' , $order['shijian']);
                                 $data['order_type'] = $order['order_type'];
                                Db::name("dingdan")->insert($data);
                                $ddh2 = substr($ddh, 0, 10);
                                $data = [];
                                $data['ip'] = $ip;
                                $data['userid'] = $userid;
                                $data['ddh'] = $ddh2;
                                $data['zyid'] = $zyid;
                                $data['ddh2'] = $ddh;
                                $data['zt'] = "已付";
                                $data['shijian'] = time();
                                $data['openid']  = $order['uopenid'];
                                $data['type'] = $order['type'];
                                // $data['zftd']     = $order['zftd'];
                                $data['uopenid']   = $order['uopenid'];
                                $data['end_time']  = time() + 86400;
                                 $data['order_type'] = $order['order_type'];
                                Db::name("ip")->insert($data);
                            } 
                            // 写自己的数据库
                        } 
                        // 不扣量
                        // 代理ID没添加扣量参数
                    }
                    else
                    { 
                        // 写自己的数据库
                        $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                        if (empty($row))
                        { 
                            // /更新会员金额
                            $user = Db::name("member")->where(['id' => $userid])->find();
                            if (!empty($user))
                            {
                                if ($cymoney < 1)
                                {
                                    $money2 = $cymoney;
                                }
                                else
                                {
                                    $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                                }
                                $money = $user['money'] + $money2;
                                Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                            }
                            $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                            // $shipin = Db::name("siyou")->where(['id' => $zyid])->find();
                            $zymc = $shipin['name'];
                            // if (!empty($shipin))
                            // {
                            //     // Db::name("siyou")->where(['id' => $zyid])->setInc("cs");
                            // Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                            // }
                            $data = [];
                            $data['zyid'] = $zyid;
                            $data['userid'] = $userid;
                            $data['zymc'] = $zymc;
                            $data['ddh'] = $ddh;
                            $data['money'] = $cymoney;
                            $data['shijian'] = $shijian;
                            $data['wxddh'] = $weixin;
                            $data['type'] = $order['type'];
                            $data['cshijian'] = date('Y-m-d H:i:s' , $order['shijian']);
                             $data['order_type'] = $order['order_type'];
                            Db::name("dingdan")->insert($data);
                            $ddh2 = substr($ddh, 0, 10);
                            $data = [];
                            $data['ip'] = $ip;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh2;
                            $data['zyid'] = $zyid;
                            $data['ddh2'] = $ddh;
                            $data['zt'] = "已付";
                            $data['shijian'] = time();
                            $data['openid']  = $order['uopenid'];
                            $data['type'] = $order['type'];
                            // $data['zftd']     = $order['zftd'];
                            $data['uopenid']  = $order['uopenid'];
                            $data['end_time']  = time() + 86400;
                             $data['order_type'] = $order['order_type'];
                            Db::name("ip")->insert($data);
                        } 
                        // 写自己的数据库
                    } 
             
        		//商户处理订单逻辑
        		echo "SUCCESS";
            }
        	
		}
        
    }
    
    
    
    public function checkorder(){
        $billNo = input('billNo','');
        $ippd = Db::name("ip")->where(['ddh2' => $billNo])->find(); //"userid"=>$userid,"ddh2"=>$ddh,
        if($ippd){
            $data['code'] = 1;
        }else{
            $data['code'] = 0;
        }
        return json($data);
    }
    
    public function yizhifu(){
        
        
         if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone')||strpos($_SERVER['HTTP_USER_AGENT'], 'iPad')){
              $ip = getIP();
          
        }else if(strpos($_SERVER['HTTP_USER_AGENT'], 'Android')){
              $ip = explode(',', getIP())[0];
        
        }else{
              $ip = getIP();
        }
       
        $ubodingdan = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES)); //提交订单号 . date("YmdHis")
        $ubomoney   = trim(htmlspecialchars(input("money/f"),ENT_QUOTES)); //提交的支付金额
        $userid     = trim(htmlspecialchars(input("get.userid"),ENT_QUOTES)); //提交的用户ID
        $ddh3       = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES));
        $zyid       = trim(htmlspecialchars(input("zyid/d"),ENT_QUOTES));
        $code       = trim(htmlspecialchars(input("get.code"),ENT_QUOTES));
        $uopenid    = trim(htmlspecialchars(input("get.uopenid",''),ENT_QUOTES));
        $pid        = trim(htmlspecialchars(input("get.pid",''),ENT_QUOTES));
        
       
        $yifutime = time() - intval(24) * 3600;
        
        $ename = Cache::store('redis')->get('ylluodi');
      

        
        if(empty($ubodingdan) || empty($ubomoney) || empty($userid) || empty($ddh3) || empty($zyid) || empty($code) || empty($uopenid)){
           
            
             die('参数错误,请重新进入？');
            
        }
        
         $info = Cache::store('redis')->get($userid);
    
        if($info == 'null' || !$info){
            $info = Db::name('dwz')->where(['ddh' => $ddh3])->find();
        }
        
         $jiami_id = $userid;
        
         $userid = $info['userid'];
         
         session('useridopenid', $uopenid);
        $liebiao = Cache::store('redis')->has('ylgongyou'.$zyid);

		if(!$liebiao || $liebiao === "null"){
		      $liebiao = Db::name("gongyou")->where(['id' => $zyid])->find();
		      if(empty($liebiao)){
		           $liebiao = Db::name("gongyou_copy1")->where(['id' => $zyid])->find();
		      }
		      Cache::store('redis')->set('ylgongyou'.$zyid,json_encode($liebiao),7879680);
		}
         
         
       
        
        $member = Cache::store('redis')->get("ylmember".$userid);
        
      
       
        $money = Db::name('pian')->where(['userid' => $userid])->value('money');
        
        
        
         
         
         if($money != $ubomoney){
            
              die('参数错误,请重新进入？');
         }
         
        //  if($userid == 10027){
             
        //  }else{
        //      $ubomoney = ceil($ubomoney); 
        //  }
         
        
        
       
        
        
        
        
        $ubodingdannew = $ubodingdan . random(4) . $userid . date("dHis");
       
        //$ubodingdannew = $ubodingdan . random(4) . $userid . date("YmdHis");
        $order = Db::name("order")->where(['orderid' => $ubodingdannew])->field('orderid,id')->find();
        
      
       
        if (empty($order))
        {
            $data = [];
            $data['orderid'] = $ubodingdannew;
            $data['ip'] = $ip;
            $data['userid'] = $userid;
            $data['zyid'] = $zyid;
            $data['shijian'] = time();
            $data['money'] = $ubomoney;
            $data['uopenid'] = $uopenid;
            $data['code']    = $code . '|' . $ubodingdan;
            $data['ddh3']    = $ddh3; 
            $data['type']    = 3; //壹支付
            $data['jiami']    = $jiami_id; 
            
            Cache::store('redis')->set($ubodingdannew,json_encode($data),86400);
            
            Db::name("order")->insert($data);
       
        }
        
      
            
            $notify_url = "http://".$ename."/index/pay/yinotify";
            
            $return_url = "http://".$ename."/index/index/shipinok?ddh=" . $ubodingdannew;
            
            
            $this->mp_pay(38, "01864eab25bd53e8e8f6c1d138c2d5e5", 900, $ubodingdannew, "测试商品", $ubomoney, $notify_url, $return_url, $mchid = '');
          
        
    }
    
    
    
    
    
    
     public function yiposturl($url, $data)
    {
        $data        = json_encode($data);
        $headerArray = array("Content-type:application/json;charset='utf-8'", "Accept:application/json");
        $curl        = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headerArray);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return json_decode($output, true);
    }
    
   
    
    
    
    
       /**
    * @Note                   支付回调
    * @param @key             对接Token
    * @param @data            返回的数据， 可以用$_REQUEST获取
    * @param @id              对接ID
    * @param @out_trade_no    发起订单号
    * @param @money           金额（元）
    * @param @trade_status    TRADE_SUCCESS or TRADE_FAIL
    */
    
    function yinotify()
    {
        $key = '01864eab25bd53e8e8f6c1d138c2d5e5';
        $data['id'] = $_REQUEST['id'];
        $data['trade_no'] = $_REQUEST['trade_no'];
        $data['out_trade_no'] = $_REQUEST['out_trade_no'];
        $data['name'] = $_REQUEST['name'];
        $data['money'] = $_REQUEST['money'];
        $data['trade_status'] = $_REQUEST['trade_status'];
        
        $data = get_magic_quotes_gpc() ? stripslashes(array_filter($data)) : array_filter($data);
        ksort($data);
        $str1 = '';
        foreach ($data as $k => $v) {
        $str1 .= '&' . $k . "=" . $v;
        }
        $sign = md5(trim($str1 . $key, '&'));
        if ($sign !== $_REQUEST['sign']) {
            
           // 失败， 失败逻辑
            echo 'fail';
            exit;
        } else {
            
        	    $shijian = date('Y-m-d H:i:s' , time());
                $out_trade_no = $data['out_trade_no']; //
                $weixin       = $data['trade_no']; //微信订单号
                $cymoney = $data['money'];
                $orderNo = $out_trade_no; //我提交的订单号
                // $openid  = $notify->data["openid"];//微信openid
                
                $order = Cache::store('redis')->get($orderNo);
        		
        		
        		if($order['money'] != $cymoney && $cymoney < 12){
        		   	 exit("签名失败");
        		}
        		
        // 		$cymoney = $cymoney * 0.97;
                
                // $order = Db::name("order")->where(['orderid' => $orderNo])->field("userid,zyid,ip,uopenid,orderid")->find();
                
                $userid = $order['userid'];
                
                
                $zyid = $order['zyid'];
                $ip = $order['ip'];
                $ddh = $orderNo;
                $qudao = $ddh;
                $ipz = Db::name("ip")->where(["ddh2" => $ddh])->find();
                if (!empty($ipz))
                {
                    exit;
                }
              
                    $kl = Db::name("kou")->where(['userid' => $userid])->find();
                    $ns = $kl['ns'];
                    if (!empty($kl))
                    {
                        $cs = $kl['cs'] - 1;
                        if ($cs <= 0)
                        {
                            
                            $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                             
                            // $shipin = Db::name("siyou")->where(['id' => $zyid])->find();
                            $zymc = $shipin['name'];
                            $user = Db::name("member")->where(['id' => $userid])->find();
                            $name = $user['nickname'];
                            $data = [];
                            $data['name'] = $name;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh;
                            $data['qudao'] = $qudao;
                            $data['money'] = $cymoney;
                            $data['shijian'] = $shijian;
                            $data['zymc'] = $zymc;
                            $data['type'] = $order['type'];
                            Db::name("kl")->insert($data);
                            Db::name("kou")->where(['userid' => $userid])->update(['cs' => $ns]);
                            // 不扣量
                            // Db::name("siyou")->where(['id'=>$zyid])->setInc("cs");
                            //  Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                            // //
                            $ddh2 = substr($ddh, 0, 10);
                            $data = [];
                            $data['ip'] = $ip;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh2;
                            $data['zyid'] = $zyid;
                            $data['ddh2'] = $ddh;
                            $data['zt'] = "已付";
                            $data['shijian'] = time();
                            $data['openid']  = $order['uopenid'];
                            $data['type'] = $order['type'];
                           
                            $data['uopenid']  = $order['uopenid'];
                           
                           
                            Db::name("ip")->insert($data);
                        }
                        else
                        {
                            Db::name("kou")->where(['userid' => $userid])->setDec("cs"); 
                            // 写自己的数据库
                            $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                            if (empty($row))
                            { 
                                // /更新会员金额
                                $user = Db::name("member")->where(['id' => $userid])->find();
                                if (!empty($user))
                                {
                                    if ($cymoney < 1)
                                    {
                                        $money2 = $cymoney;
                                    }
                                    else
                                    {
                                        $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                                    }
                                    $money = $user['money'] + $money2;
                                    Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                                }
                                
                            
                                $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                                $zymc = $shipin['name'];
                                // if (!empty($shipin))
                                // {
                                //     // Db::name("siyou")->where(['id' => $zyid])->setInc("cs");
                                //  Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                                // }
                                $data = [];
                                $data['zyid'] = $zyid;
                                $data['userid'] = $userid;
                                $data['zymc'] = $zymc;
                                $data['ddh'] = $ddh;
                                $data['money'] = $cymoney;
                                $data['shijian'] = $shijian;
                                $data['wxddh'] = $weixin;
                                $data['type'] = $order['type'];
                                Db::name("dingdan")->insert($data);
                                $ddh2 = substr($ddh, 0, 10);
                                $data = [];
                                $data['ip'] = $ip;
                                $data['userid'] = $userid;
                                $data['ddh'] = $ddh2;
                                $data['zyid'] = $zyid;
                                $data['ddh2'] = $ddh;
                                $data['zt'] = "已付";
                                $data['shijian'] = time();
                                $data['openid']  = $order['uopenid'];
                                $data['type'] = $order['type'];
                                $data['uopenid']  = $order['uopenid'];
                           
                                Db::name("ip")->insert($data);
                            } 
                            // 写自己的数据库
                        } 
                        // 不扣量
                        // 代理ID没添加扣量参数
                    }
                    else
                    { 
                        // 写自己的数据库
                        $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                        if (empty($row))
                        { 
                            // /更新会员金额
                            $user = Db::name("member")->where(['id' => $userid])->find();
                            if (!empty($user))
                            {
                                if ($cymoney < 1)
                                {
                                    $money2 = $cymoney;
                                }
                                else
                                {
                                    $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                                }
                                $money = $user['money'] + $money2;
                                Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                            }
                            $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                            // $shipin = Db::name("siyou")->where(['id' => $zyid])->find();
                            $zymc = $shipin['name'];
                            // if (!empty($shipin))
                            // {
                            //     // Db::name("siyou")->where(['id' => $zyid])->setInc("cs");
                            // Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                            // }
                            $data = [];
                            $data['zyid'] = $zyid;
                            $data['userid'] = $userid;
                            $data['zymc'] = $zymc;
                            $data['ddh'] = $ddh;
                            $data['money'] = $cymoney;
                            $data['shijian'] = $shijian;
                            $data['wxddh'] = $weixin;
                            $data['type'] = $order['type'];
                            Db::name("dingdan")->insert($data);
                            $ddh2 = substr($ddh, 0, 10);
                            $data = [];
                            $data['ip'] = $ip;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh2;
                            $data['zyid'] = $zyid;
                            $data['ddh2'] = $ddh;
                            $data['zt'] = "已付";
                            $data['shijian']  = time();
                            $data['openid']   = $order['uopenid'];
                            $data['uopenid']  = $order['uopenid'];
                            $data['type'] = $order['type'];
                            Db::name("ip")->insert($data);
                        } 
                        // 写自己的数据库
                    } 
          
            // 成功， 成功逻辑
            echo 'success';
        }
     }
     
     
    //彩虹易支付
    public function caihongpay(){
        
        
         if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone')||strpos($_SERVER['HTTP_USER_AGENT'], 'iPad')){
              $ip = getIP();
          
        }else if(strpos($_SERVER['HTTP_USER_AGENT'], 'Android')){
              $ip = explode(',', getIP())[0];
        
        }else{
              $ip = getIP();
        }
       
        $ubodingdan = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES)); //提交订单号 . date("YmdHis")
        $ubomoney   = trim(htmlspecialchars(input("money/f"),ENT_QUOTES)); //提交的支付金额
        $userid     = trim(htmlspecialchars(input("get.userid"),ENT_QUOTES)); //提交的用户ID
        $ddh3       = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES));
        $zyid       = trim(htmlspecialchars(input("zyid/d"),ENT_QUOTES));
        $code       = trim(htmlspecialchars(input("get.code"),ENT_QUOTES));
        $uopenid    = trim(htmlspecialchars(input("get.uopenid",''),ENT_QUOTES));
        $pid        = trim(htmlspecialchars(input("get.pid",''),ENT_QUOTES));
        $is_baotian = trim(htmlspecialchars(input("get.is_baotian"),ENT_QUOTES));
        $b_money    = trim(htmlspecialchars(input("get.b_money"),ENT_QUOTES));
        $yifutime   = time() - intval(24) * 3600;
        
        $ename = Cache::store('redis')->get('ylluodi');
      

        
        if(empty($ubodingdan) || empty($ubomoney) || empty($userid) || empty($ddh3) || empty($zyid) || empty($code) || empty($uopenid)){
           
            
             die('参数错误,请重新进入？');
            
        }
        
         //查询是否购买
        session('useridopenid', $uopenid);
        
         $info = Db::name('baotian')->where(['uopenid' => $uopenid])->order('id desc')->find();
        
        if($info && $info['end_time'] > time()){
           
            $geturl = "http://".$ename."/index/index/shipinok.html?zyid=" . $info['zyid'] ."&ddh=".$info['orderid']."&uopenid=".$uopenid."&bzyid=".$zyid;
            
          
            $this->redirect($geturl);
        }
        
        
        
         $info = Cache::store('redis')->get($userid);
    
        if($info == 'null' || !$info){
            $info = Db::name('dwz')->where(['ddh' => $ddh3])->find();
        }
        
         $jiami_id = $userid;
        
         $userid = $info['userid'];
         
         
        $liebiao = Cache::store('redis')->has('ylgongyou'.$zyid);

		if(!$liebiao || $liebiao === "null"){
		      $liebiao = Db::name("gongyou")->where(['id' => $zyid])->find();
		      if(empty($liebiao)){
		           $liebiao = Db::name("gongyou_copy1")->where(['id' => $zyid])->find();
		      }
		      Cache::store('redis')->set('ylgongyou'.$zyid,json_encode($liebiao),7879680);
		}
         
         
       
        
        $member = Cache::store('redis')->get("member".$userid);
        
      
       
          if($b_money >= 30 && $is_baotian == 1){
            
             
              $order_type = 2;
              $money      = Db::name('pian')->where(['userid' => $userid])->value('b_money');
             
              if($money != $b_money){
                 die('参数错误');
              }
             
              $ubomoney = $money; 
          
             
        }else{
             $order_type = 1;
             $ubomoney = Db::name('pian')->where(['userid' => $userid])->value('money');
            
            
        }
        
        
        $ubodingdannew = $ubodingdan . random(4) . $userid . date("dHis");
       
        //$ubodingdannew = $ubodingdan . random(4) . $userid . date("YmdHis");
        $order = Db::name("order")->where(['orderid' => $ubodingdannew])->field('orderid,id')->find();
        
      
       
        if (empty($order))
        {
            $data = [];
            $data['orderid'] = $ubodingdannew;
            $data['ip'] = $ip;
            $data['userid'] = $userid;
            $data['zyid'] = $zyid;
            $data['shijian'] = time();
            $data['money'] = $ubomoney;
            $data['uopenid'] = $uopenid;
            $data['code']    = $code . '|' . $ubodingdan;
            $data['ddh3']    = $ddh3; 
            $data['type']    = 8; //彩虹支付
            $data['jiami']    = $jiami_id; 
            $data['order_type']    = $order_type; 
            
            Cache::store('redis')->set($ubodingdannew,json_encode($data),86400);
            
            Db::name("order")->insert($data);
       
        }
        
        $ubomoney = ceil($ubomoney);
         
                // Loader::import('caihong.epay.config.php');
                // Loader::import('caihong.lib.epay.epay_submit.class.php');
                require_once("./extend/caihong/epay.config.php");
                require_once("./extend/caihong/lib/epay_submit.class.php");
        
                /**************************请求参数**************************/
                
                $notify_url = "http://".$ename."/index/pay/caihongnotify";
                $return_url = "http://" . $ename . "/index/index/shipinok.html?ddh=" . $ubodingdannew;
                // $notify_url = "http://pay.52o.ee/SDK/notify_url.php";
                //需http://格式的完整路径，不能加?id=123这类自定义参数
        
                //页面跳转同步通知页面路径
                // $return_url = "http://pay.52o.ee/SDK/return_url.php";
                //需http://格式的完整路径，不能加?id=123这类自定义参数，不能写成http://localhost/
        
                //商户订单号
                $out_trade_no = $ubodingdannew;
                //商户网站订单系统中唯一订单号，必填
        
        		//支付方式
                $type    = "wxpay";
                //商品名称
                $name    = "测试商品";
        		//付款金额
                $money   = $ubomoney;
        		//站点名称
                $sitename = '彩虹易支付测试站点';
                //必填
        
                //订单描述
        
        /************************************************************/
        //构造要请求的参数数组，无需改动
        $parameter = array(
        		"pid" => 1003,
        		"type" => $type,
        		"notify_url"	=> $notify_url,
        		"return_url"	=> $return_url,
        		"out_trade_no"	=> $out_trade_no,
        		"name"	=> $name,
        		"money"	=> $money,
        		"sitename"	=> $sitename
        );
        
        //建立请求
        $alipaySubmit = new \AlipaySubmit($alipay_config);
        $html_text = $alipaySubmit->buildRequestForm($parameter);
        
        echo  $html_text;
        
       
        
        
        // $this->mp_pay(38, "01864eab25bd53e8e8f6c1d138c2d5e5", 900, $ubodingdannew, "测试商品", $ubomoney, $notify_url, $return_url, $mchid = '');
          
        
    }
    
    
    public function  caihongnotify(){
        require_once("./extend/caihong/epay.config.php");
        require_once("./extend/caihong/lib/epay_notify.class.php");
        
        //计算得出通知验证结果
        $alipayNotify = new \AlipayNotify($alipay_config);
        $verify_result = $alipayNotify->verifyNotify();
        if($verify_result) {//验证成功
            //获取支付宝的通知返回参数，可参考技术文档中服务器异步通知参数列表
        	//商户订单号
        	$out_trade_no  = $_GET['out_trade_no'];
        
        	//彩虹易支付交易号
        
        	$trade_no      = $_GET['trade_no'];
        
        	//交易状态
        	$trade_status  = $_GET['trade_status'];
        
        	//支付方式
        	$type = $_GET['type'];
        
        
        	if ($_GET['trade_status'] == 'TRADE_SUCCESS') {
        	    
        	    
        	    $trade_no     = $trade_no;//彩虹订单号
            	$out_trade_no = $out_trade_no;//系统订单号
                $money        = $_GET['money'];//订单金额
    
    	    
        	    $shijian = date('Y-m-d H:i:s' , time());
                $out_trade_no = $out_trade_no; //
                $weixin       = $trade_no; //微信订单号
                $cymoney = $money;
                $orderNo = $out_trade_no; //我提交的订单号
                // $openid  = $notify->data["openid"];//微信openid
                
                $order = Cache::store('redis')->get($orderNo);
        		
        		
        		if($order['money'] != $cymoney ){
        		   	 exit("签名失败");
        		}
        		
        // 		$cymoney = $cymoney * 0.97;
                
                // $order = Db::name("order")->where(['orderid' => $orderNo])->field("userid,zyid,ip,uopenid,orderid")->find();
                
                $userid = $order['userid'];
                
                
                $zyid = $order['zyid'];
                $ip = $order['ip'];
                $ddh = $orderNo;
                $qudao = $ddh;
                $ipz = Db::name("ip")->where(["ddh2" => $ddh])->find();
                if (!empty($ipz))
                {
                    exit;
                }
                
                if($order['order_type'] == 2){
                    Db::name('baotian')->insert(['userid' => $order['userid'],'zyid' => $order['zyid'],'money' => $order['money'],'uopenid' => $order['uopenid'],'create_time' => time(),'end_time' => time() + 86400,'orderid' => $orderNo]);
                }
              
                    $kl = Db::name("kou")->where(['userid' => $userid])->find();
                    $ns = $kl['ns'];
                    if (!empty($kl))
                    {
                        $cs = $kl['cs'] - 1;
                        if ($cs <= 0)
                        {
                            
                            $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                             
                            // $shipin = Db::name("siyou")->where(['id' => $zyid])->find();
                            $zymc = $shipin['name'];
                            $user = Db::name("member")->where(['id' => $userid])->find();
                            $name = $user['nickname'];
                            $data = [];
                            $data['name'] = $name;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh;
                            $data['qudao'] = $qudao;
                            $data['money'] = $cymoney;
                            $data['shijian'] = $shijian;
                            $data['zymc'] = $zymc;
                            $data['type'] = $order['type'];
                            $data['order_type'] = $order['order_type'];
                            Db::name("kl")->insert($data);
                            Db::name("kou")->where(['userid' => $userid])->update(['cs' => $ns]);
                            // 不扣量
                            // Db::name("siyou")->where(['id'=>$zyid])->setInc("cs");
                            //  Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                            // //
                            $ddh2 = substr($ddh, 0, 10);
                            $data = [];
                            $data['ip'] = $ip;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh2;
                            $data['zyid'] = $zyid;
                            $data['ddh2'] = $ddh;
                            $data['zt'] = "已付";
                            $data['shijian'] = time();
                            $data['openid']  = $order['uopenid'];
                            $data['type']    = $order['type'];
                            $data['order_type'] = $order['order_type'];
                            $data['uopenid']  = $order['uopenid'];
                            $data['end_time']  = time() + 86400;
                           
                            Db::name("ip")->insert($data);
                        }
                        else
                        {
                            Db::name("kou")->where(['userid' => $userid])->setDec("cs"); 
                            // 写自己的数据库
                            $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                            if (empty($row))
                            { 
                                // /更新会员金额
                                $user = Db::name("member")->where(['id' => $userid])->find();
                                if (!empty($user))
                                {
                                    if ($cymoney < 1)
                                    {
                                        $money2 = $cymoney;
                                    }
                                    else
                                    {
                                        $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                                    }
                                    $money = $user['money'] + $money2;
                                    Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                                }
                                
                            
                                $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                                $zymc = $shipin['name'];
                                // if (!empty($shipin))
                                // {
                                //     // Db::name("siyou")->where(['id' => $zyid])->setInc("cs");
                                //  Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                                // }
                                $data = [];
                                $data['zyid'] = $zyid;
                                $data['userid'] = $userid;
                                $data['zymc'] = $zymc;
                                $data['ddh'] = $ddh;
                                $data['money'] = $cymoney;
                                $data['shijian'] = $shijian;
                                $data['wxddh'] = $weixin;
                                $data['type']    = $order['type'];
                                $data['order_type'] = $order['order_type'];
                                Db::name("dingdan")->insert($data);
                                $ddh2 = substr($ddh, 0, 10);
                                $data = [];
                                $data['ip'] = $ip;
                                $data['userid'] = $userid;
                                $data['ddh'] = $ddh2;
                                $data['zyid'] = $zyid;
                                $data['ddh2'] = $ddh;
                                $data['zt'] = "已付";
                                $data['shijian'] = time();
                                $data['openid']  = $order['uopenid'];
                                $data['type']    = $order['type'];
                                $data['uopenid']  = $order['uopenid'];
                                $data['order_type'] = $order['order_type'];
                                 $data['end_time']  = time() + 86400;
                                Db::name("ip")->insert($data);
                            } 
                            // 写自己的数据库
                        } 
                        // 不扣量
                        // 代理ID没添加扣量参数
                    }
                    else
                    { 
                        // 写自己的数据库
                        $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                        if (empty($row))
                        { 
                            // /更新会员金额
                            $user = Db::name("member")->where(['id' => $userid])->find();
                            if (!empty($user))
                            {
                                if ($cymoney < 1)
                                {
                                    $money2 = $cymoney;
                                }
                                else
                                {
                                    $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                                }
                                $money = $user['money'] + $money2;
                                Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                            }
                            $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                            // $shipin = Db::name("siyou")->where(['id' => $zyid])->find();
                            $zymc = $shipin['name'];
                            // if (!empty($shipin))
                            // {
                            //     // Db::name("siyou")->where(['id' => $zyid])->setInc("cs");
                            // Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                            // }
                            $data = [];
                            $data['zyid'] = $zyid;
                            $data['userid'] = $userid;
                            $data['zymc'] = $zymc;
                            $data['ddh'] = $ddh;
                            $data['money'] = $cymoney;
                            $data['shijian'] = $shijian;
                            $data['wxddh'] = $weixin;
                            $data['type']    = $order['type'];
                            $data['order_type'] = $order['order_type'];
                            
                            Db::name("dingdan")->insert($data);
                            $ddh2 = substr($ddh, 0, 10);
                            $data = [];
                            $data['ip'] = $ip;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh2;
                            $data['zyid'] = $zyid;
                            $data['ddh2'] = $ddh;
                            $data['zt'] = "已付";
                            $data['shijian']  = time();
                            $data['openid']   = $order['uopenid'];
                            $data['uopenid']  = $order['uopenid'];
                            $data['type']     = $order['type'];
                            $data['order_type'] = $order['order_type'];
                            $data['end_time']  = time() + 86400;
                            Db::name("ip")->insert($data);
                        } 
                        // 写自己的数据库
                    } 
        	    
        	    
        		//判断该笔订单是否在商户网站中已经做过处理
        			//如果没有做过处理，根据订单号（out_trade_no）在商户网站的订单系统中查到该笔订单的详细，并执行商户的业务程序
        			//请务必判断请求时的total_fee、seller_id与通知时获取的total_fee、seller_id为一致的
        			//如果有做过处理，不执行商户的业务程序
        				
        		//注意：
        		//付款完成后，支付宝系统发送该交易状态通知
        
            }
        
        	//——请根据您的业务逻辑来编写程序（以上代码仅作参考）——
                
        	echo "success";		//请不要修改或删除
        	
        	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        }
        else {
            //验证失败
            echo "fail";
        }

		
        
    }
    
    
    public function newpayca(){
        
        
         if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone')||strpos($_SERVER['HTTP_USER_AGENT'], 'iPad')){
              $ip = getIP();
          
        }else if(strpos($_SERVER['HTTP_USER_AGENT'], 'Android')){
              $ip = explode(',', getIP())[0];
        
        }else{
              $ip = getIP();
        }
       
        $ubodingdan = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES)); //提交订单号 . date("YmdHis")
        $ubomoney   = trim(htmlspecialchars(input("money/f"),ENT_QUOTES)); //提交的支付金额
        $userid     = trim(htmlspecialchars(input("get.userid"),ENT_QUOTES)); //提交的用户ID
        $ddh3       = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES));
        $zyid       = trim(htmlspecialchars(input("zyid/d"),ENT_QUOTES));
        $code       = trim(htmlspecialchars(input("get.code"),ENT_QUOTES));
        $uopenid    = trim(htmlspecialchars(input("get.uopenid",''),ENT_QUOTES));
        $pid        = trim(htmlspecialchars(input("get.pid",''),ENT_QUOTES));
        
       
        $yifutime = time() - intval(24) * 3600;
        
        $ename = Cache::store('redis')->get('ylluodi');
      

        
        if(empty($ubodingdan) || empty($ubomoney) || empty($userid) || empty($ddh3) || empty($zyid) || empty($code) || empty($uopenid)){
           
            
             die('参数错误,请重新进入？');
            
        }
        
         $info = Cache::store('redis')->get($userid);
    
        if($info == 'null' || !$info){
            $info = Db::name('dwz')->where(['ddh' => $ddh3])->find();
        }
        
         $jiami_id = $userid;
        
         $userid = $info['userid'];
         
         
        $liebiao = Cache::store('redis')->has('ylgongyou'.$zyid);

		if(!$liebiao || $liebiao === "null"){
		      $liebiao = Db::name("gongyou")->where(['id' => $zyid])->find();
		      if(empty($liebiao)){
		           $liebiao = Db::name("gongyou_copy1")->where(['id' => $zyid])->find();
		      }
		      Cache::store('redis')->set('ylgongyou'.$zyid,json_encode($liebiao),7879680);
		}
         
         
       
        
        $member = Cache::store('redis')->get("member".$userid);
        
      
       
        $money = Db::name('pian')->where(['userid' => $userid])->value('money');
        
        
        
         
         
         if($money != $ubomoney){
            
              die('参数错误,请重新进入？');
         }
         
        //  if($userid == 10027){
             
        //  }else{
        //      $ubomoney = ceil($ubomoney); 
        //  }
         
        
        
       
        
        
        
        
        $ubodingdannew = $ubodingdan . random(4) . $userid . date("dHis");
       
        //$ubodingdannew = $ubodingdan . random(4) . $userid . date("YmdHis");
        $order = Db::name("order")->where(['orderid' => $ubodingdannew])->field('orderid,id')->find();
        
      
       
        if (empty($order))
        {
            $data = [];
            $data['orderid'] = $ubodingdannew;
            $data['ip'] = $ip;
            $data['userid'] = $userid;
            $data['zyid'] = $zyid;
            $data['shijian'] = time();
            $data['money'] = $ubomoney;
            $data['uopenid'] = $uopenid;
            $data['code']    = $code . '|' . $ubodingdan;
            $data['ddh3']    = $ddh3; 
            $data['type']    = 8; //彩虹支付
            $data['jiami']    = $jiami_id; 
            
            Cache::store('redis')->set($ubodingdannew,json_encode($data),86400);
            
            Db::name("order")->insert($data);
       
        }
        
         
                // Loader::import('caihong.epay.config.php');
                // Loader::import('caihong.lib.epay.epay_submit.class.php');
                require_once("./extend/newpay/epay.config.php");
                require_once("./extend/newpay/lib/epay_submit.class.php");
        
                /**************************请求参数**************************/
                
                $notify_url = "http://".$ename."/index/pay/newpaynotify";
                $return_url = "http://" . $ename . "/index/index/shipinok.html?ddh=" . $ubodingdannew;
                // $notify_url = "http://pay.52o.ee/SDK/notify_url.php";
                //需http://格式的完整路径，不能加?id=123这类自定义参数
        
                //页面跳转同步通知页面路径
                // $return_url = "http://pay.52o.ee/SDK/return_url.php";
                //需http://格式的完整路径，不能加?id=123这类自定义参数，不能写成http://localhost/
        
                //商户订单号
                $out_trade_no = $ubodingdannew;
                //商户网站订单系统中唯一订单号，必填
        
        		//支付方式
                $type    = "wxpay";
                //商品名称
                $name    = "测试商品";
        		//付款金额
                $money   = $ubomoney;
        		//站点名称
                $sitename = '彩虹易支付测试站点';
                //必填
        
                //订单描述
        
        /************************************************************/
        //构造要请求的参数数组，无需改动
        $parameter = array(
        		"pid" => 1060,
        		"type" => $type,
        		"notify_url"	=> $notify_url,
        		"return_url"	=> $return_url,
        		"out_trade_no"	=> $out_trade_no,
        		"name"	=> $name,
        		"money"	=> $money,
        		"sitename"	=> $sitename
        );
        
        //建立请求
        $alipaySubmit = new \AlipaySubmit($alipay_config);
        $html_text = $alipaySubmit->buildRequestForm($parameter);
        
        echo  $html_text;
        
       
        
        
        // $this->mp_pay(38, "01864eab25bd53e8e8f6c1d138c2d5e5", 900, $ubodingdannew, "测试商品", $ubomoney, $notify_url, $return_url, $mchid = '');
          
        
    }
    
    
    public function  newpaynotify(){
        require_once("./extend/newpay/epay.config.php");
        require_once("./extend/newpay/lib/epay_notify.class.php");
        
        //计算得出通知验证结果
        $alipayNotify = new \AlipayNotify($alipay_config);
        $verify_result = $alipayNotify->verifyNotify();
        if($verify_result) {//验证成功
            //获取支付宝的通知返回参数，可参考技术文档中服务器异步通知参数列表
        	//商户订单号
        	$out_trade_no  = $_GET['out_trade_no'];
        
        	//彩虹易支付交易号
        
        	$trade_no      = $_GET['trade_no'];
        
        	//交易状态
        	$trade_status  = $_GET['trade_status'];
        
        	//支付方式
        	$type = $_GET['type'];
        
        
        	if ($_GET['trade_status'] == 'TRADE_SUCCESS') {
        	    
        	    
        	    $trade_no     = $trade_no;//彩虹订单号
            	$out_trade_no = $out_trade_no;//系统订单号
                $money        = $_GET['money'];//订单金额
    
    	    
        	    $shijian = date('Y-m-d H:i:s' , time());
                $out_trade_no = $out_trade_no; //
                $weixin       = $trade_no; //微信订单号
                $cymoney = $money;
                $orderNo = $out_trade_no; //我提交的订单号
                // $openid  = $notify->data["openid"];//微信openid
                
                $order = Cache::store('redis')->get($orderNo);
        		
        		
        		if($order['money'] != $cymoney && $cymoney < 12){
        		   	 exit("签名失败");
        		}
        		
        // 		$cymoney = $cymoney * 0.97;
                
                // $order = Db::name("order")->where(['orderid' => $orderNo])->field("userid,zyid,ip,uopenid,orderid")->find();
                
                $userid = $order['userid'];
                
                
                $zyid = $order['zyid'];
                $ip = $order['ip'];
                $ddh = $orderNo;
                $qudao = $ddh;
                $ipz = Db::name("ip")->where(["ddh2" => $ddh])->find();
                if (!empty($ipz))
                {
                    exit;
                }
              
                    $kl = Db::name("kou")->where(['userid' => $userid])->find();
                    $ns = $kl['ns'];
                    if (!empty($kl))
                    {
                        $cs = $kl['cs'] - 1;
                        if ($cs <= 0)
                        {
                            
                            $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                             
                            // $shipin = Db::name("siyou")->where(['id' => $zyid])->find();
                            $zymc = $shipin['name'];
                            $user = Db::name("member")->where(['id' => $userid])->find();
                            $name = $user['nickname'];
                            $data = [];
                            $data['name'] = $name;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh;
                            $data['qudao'] = $qudao;
                            $data['money'] = $cymoney;
                            $data['shijian'] = $shijian;
                            $data['zymc'] = $zymc;
                            $data['type'] = $order['type'];
                            Db::name("kl")->insert($data);
                            Db::name("kou")->where(['userid' => $userid])->update(['cs' => $ns]);
                            // 不扣量
                            // Db::name("siyou")->where(['id'=>$zyid])->setInc("cs");
                            //  Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                            // //
                            $ddh2 = substr($ddh, 0, 10);
                            $data = [];
                            $data['ip'] = $ip;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh2;
                            $data['zyid'] = $zyid;
                            $data['ddh2'] = $ddh;
                            $data['zt'] = "已付";
                            $data['shijian'] = time();
                            $data['openid']  = $order['uopenid'];
                            $data['type']    = $order['type'];
                           
                            $data['uopenid']  = $order['uopenid'];
                           
                           
                            Db::name("ip")->insert($data);
                        }
                        else
                        {
                            Db::name("kou")->where(['userid' => $userid])->setDec("cs"); 
                            // 写自己的数据库
                            $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                            if (empty($row))
                            { 
                                // /更新会员金额
                                $user = Db::name("member")->where(['id' => $userid])->find();
                                if (!empty($user))
                                {
                                    if ($cymoney < 1)
                                    {
                                        $money2 = $cymoney;
                                    }
                                    else
                                    {
                                        $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                                    }
                                    $money = $user['money'] + $money2;
                                    Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                                }
                                
                            
                                $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                                $zymc = $shipin['name'];
                                // if (!empty($shipin))
                                // {
                                //     // Db::name("siyou")->where(['id' => $zyid])->setInc("cs");
                                //  Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                                // }
                                $data = [];
                                $data['zyid'] = $zyid;
                                $data['userid'] = $userid;
                                $data['zymc'] = $zymc;
                                $data['ddh'] = $ddh;
                                $data['money'] = $cymoney;
                                $data['shijian'] = $shijian;
                                $data['wxddh'] = $weixin;
                                $data['type']    = $order['type'];
                                Db::name("dingdan")->insert($data);
                                $ddh2 = substr($ddh, 0, 10);
                                $data = [];
                                $data['ip'] = $ip;
                                $data['userid'] = $userid;
                                $data['ddh'] = $ddh2;
                                $data['zyid'] = $zyid;
                                $data['ddh2'] = $ddh;
                                $data['zt'] = "已付";
                                $data['shijian'] = time();
                                $data['openid']  = $order['uopenid'];
                                $data['type']    = $order['type'];
                                $data['uopenid']  = $order['uopenid'];
                           
                                Db::name("ip")->insert($data);
                            } 
                            // 写自己的数据库
                        } 
                        // 不扣量
                        // 代理ID没添加扣量参数
                    }
                    else
                    { 
                        // 写自己的数据库
                        $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                        if (empty($row))
                        { 
                            // /更新会员金额
                            $user = Db::name("member")->where(['id' => $userid])->find();
                            if (!empty($user))
                            {
                                if ($cymoney < 1)
                                {
                                    $money2 = $cymoney;
                                }
                                else
                                {
                                    $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                                }
                                $money = $user['money'] + $money2;
                                Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                            }
                            $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                            // $shipin = Db::name("siyou")->where(['id' => $zyid])->find();
                            $zymc = $shipin['name'];
                            // if (!empty($shipin))
                            // {
                            //     // Db::name("siyou")->where(['id' => $zyid])->setInc("cs");
                            // Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                            // }
                            $data = [];
                            $data['zyid'] = $zyid;
                            $data['userid'] = $userid;
                            $data['zymc'] = $zymc;
                            $data['ddh'] = $ddh;
                            $data['money'] = $cymoney;
                            $data['shijian'] = $shijian;
                            $data['wxddh'] = $weixin;
                            $data['type']    = $order['type'];
                            Db::name("dingdan")->insert($data);
                            $ddh2 = substr($ddh, 0, 10);
                            $data = [];
                            $data['ip'] = $ip;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh2;
                            $data['zyid'] = $zyid;
                            $data['ddh2'] = $ddh;
                            $data['zt'] = "已付";
                            $data['shijian']  = time();
                            $data['openid']   = $order['uopenid'];
                            $data['uopenid']  = $order['uopenid'];
                            $data['type']     = $order['type'];
                            Db::name("ip")->insert($data);
                        } 
                        // 写自己的数据库
                    } 
        	    
        	    
        		//判断该笔订单是否在商户网站中已经做过处理
        			//如果没有做过处理，根据订单号（out_trade_no）在商户网站的订单系统中查到该笔订单的详细，并执行商户的业务程序
        			//请务必判断请求时的total_fee、seller_id与通知时获取的total_fee、seller_id为一致的
        			//如果有做过处理，不执行商户的业务程序
        				
        		//注意：
        		//付款完成后，支付宝系统发送该交易状态通知
        
            }
        
        	//——请根据您的业务逻辑来编写程序（以上代码仅作参考）——
                
        	echo "success";		//请不要修改或删除
        	
        	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        }
        else {
            //验证失败
            echo "fail";
        }

		
        
    }
    
    
    //彩虹支付通知
    
    // public function caihongreturn(){
    //     require_once("./extend/caihong/epay.config.php");
    //     require_once("./extend/caihong/lib/epay_notify.class.php");
   
    //     $alipayNotify = new \AlipayNotify($alipay_config);
    //     $verify_result = $alipayNotify->verifyReturn();
    //     if($verify_result) {//验证成功
    //     	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //     	//请在这里加上商户的业务逻辑程序代码
        	
    //     	//——请根据您的业务逻辑来编写程序（以下代码仅作参考）——
    //         //获取支付宝的通知返回参数，可参考技术文档中页面跳转同步通知参数列表
        
    //     	//商户订单号
        
    //     	$out_trade_no = $_GET['out_trade_no'];
        
    //     	//支付宝交易号
        
    //     	$trade_no     = $_GET['trade_no'];
        
    //     	//交易状态
    //     	$trade_status = $_GET['trade_status'];
        
    //     	//支付方式
    //     	$type = $_GET['type'];
        
        
    //         if($_GET['trade_status'] == 'TRADE_SUCCESS') {
        		
    //         }
    //         else {
    //           echo "trade_status=".$_GET['trade_status'];
    //         }
            
    //         $ename = Cache::store('redis')->get('ylluodi');
    //         $shiping = "http://".$ename."/index/index/shipinok?ddh=" . $out_trade_no;
    //         $ippd = Db::name("ip")->where(['ddh2' => $out_trade_no])->find(); //"userid"=>$userid,"ddh2"=>$ddh,
        
    //         if($ippd){
    //              $this->redirect($shiping);
    //         }
        
    //     	//——请根据您的业务逻辑来编写程序（以上代码仅作参考）——
        	
    //     	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //     }
    //     else {
    //         //验证失败
    //         //如要调试，请看alipay_notify.php页面的verifyReturn函数
    //         echo "验证失败";
    //     }
        
        
    // }
     
     
      //黄秋生支付开关
     public function huangqiuspay(){
        
        
         if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone')||strpos($_SERVER['HTTP_USER_AGENT'], 'iPad')){
              $ip = getIP();
          
        }else if(strpos($_SERVER['HTTP_USER_AGENT'], 'Android')){
              $ip = explode(',', getIP())[0];
        
        }else{
              $ip = getIP();
        }
       
        $ubodingdan = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES)); //提交订单号 . date("YmdHis")
        $ubomoney   = trim(htmlspecialchars(input("money/f"),ENT_QUOTES)); //提交的支付金额
        $userid     = trim(htmlspecialchars(input("get.userid"),ENT_QUOTES)); //提交的用户ID
        $ddh3       = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES));
        $zyid       = trim(htmlspecialchars(input("zyid/d"),ENT_QUOTES));
        $code       = trim(htmlspecialchars(input("get.code"),ENT_QUOTES));
        $uopenid    = trim(htmlspecialchars(input("get.uopenid",''),ENT_QUOTES));
        $pid        = trim(htmlspecialchars(input("get.pid",''),ENT_QUOTES));
        
       
        $yifutime = time() - intval(24) * 3600;
        
        $ename = Cache::store('redis')->get('ylluodi');
      

        
        if(empty($ubodingdan) || empty($ubomoney) || empty($userid) || empty($ddh3) || empty($zyid) || empty($code) || empty($uopenid)){
           
            
             die('参数错误,请重新进入？');
            
        }
        
         $info = Cache::store('redis')->get($userid);
    
        if($info == 'null' || !$info){
            $info = Db::name('dwz')->where(['ddh' => $ddh3])->find();
        }
        
         $jiami_id = $userid;
        
         $userid = $info['userid'];
         
         
        $liebiao = Cache::store('redis')->has('ylgongyou'.$zyid);

		if(!$liebiao || $liebiao === "null"){
		      $liebiao = Db::name("gongyou")->where(['id' => $zyid])->find();
		      if(empty($liebiao)){
		           $liebiao = Db::name("gongyou_copy1")->where(['id' => $zyid])->find();
		      }
		      Cache::store('redis')->set('ylgongyou'.$zyid,json_encode($liebiao),7879680);
		}
         
         
       
        
        $member = Cache::store('redis')->get("ylmember".$userid);
        
      
       
        $money = Db::name('pian')->where(['userid' => $userid])->value('money');
        
        
        
         
         
         if($money != $ubomoney){
            
              die('参数错误,请重新进入？');
         }
         
        //  if($userid == 10027){
             
        //  }else{
        //      $ubomoney = ceil($ubomoney); 
        //  }
         
        
        
       
        
        
        
        
        $ubodingdannew = $ubodingdan . random(4) . $userid . date("dHis");
       
        //$ubodingdannew = $ubodingdan . random(4) . $userid . date("YmdHis");
        $order = Db::name("order")->where(['orderid' => $ubodingdannew])->field('orderid,id')->find();
        
      
       
        if (empty($order))
        {
            $data = [];
            $data['orderid'] = $ubodingdannew;
            $data['ip'] = $ip;
            $data['userid'] = $userid;
            $data['zyid'] = $zyid;
            $data['shijian'] = time();
            $data['money'] = $ubomoney;
            $data['uopenid'] = $uopenid;
            $data['code']    = $code . '|' . $ubodingdan;
            $data['ddh3']    = $ddh3; 
            $data['type']    = 6; //黄秋生支付
            $data['jiami']    = $jiami_id; 
            
            Cache::store('redis')->set($ubodingdannew,json_encode($data),86400);
            
            Db::name("order")->insert($data);
       
        }
        
            $payurl = "http://139.224.226.17:8000/order/payment/create";
            
            $notify_url = "http://".$ename."/index/pay/huangqiusnotify";
    
            $return_url = "http://".$ename."/index/index/shipinok?ddh=" . $ubodingdannew;
            
            $merchantKey = "11d09490fcd5409fa2f84176598ea704";
            
           
            $body['amount'] = $ubomoney * 100;
            $body['channelType'] = "2:1";
            $body['merchantOrderCode'] = $ubodingdannew;
            $body['noticeUrl'] = $notify_url;
            $body['returnUrl'] = $return_url;
            
            $signStr = '';
            foreach ($body as $key => $value){
              $signStr .= $key . '=' . $value;
            }
            $signStr .= 'key=' . $merchantKey;
            
            // var_dump($signStr);
            
            $paydata['merchantCode'] = "1606400277902";
            $paydata['body'] = $body;
            // $signdata = 'amount='.$body['amount'].'channelType='.$body['channelType'].'merchantOrderCode='.$paydata['merchantCode'].'noticeUrl='.$body['noticeUrl'].'returnUrl='. $return_url.'key='.$key;
            $sign = strtolower(md5($signStr));
            $paydata['sign'] = $sign;
            
            $ch = curl_init($payurl);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($paydata));
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
            $response = curl_exec($ch);
            curl_close($ch);
            $resdata = json_decode($response,true);
            
            if($resdata['code'] == 200){
             
                 $this->redirect($resdata['body']['paymentUrl']);
            }
            
        
    }
    
    
     function huangqiusnotify()
    {
        
       
        
        $merchantKey = "11d09490fcd5409fa2f84176598ea704";
        
        
        
        
        $post = file_get_contents('php://input');
        
        
        $post = json_decode($post,true);
        
       
       
        $huangdata['amount']            = $post['amount'];
        $huangdata['merchantOrderCode'] = $post['merchantOrderCode'];//商户订单号-020+0
        $huangdata['platformOrderCode'] = $post['platformOrderCode'];
        $huangdata['status']            = $post['status'];
       
       
        $log_name = "huang.log"; //log文件路径
        
       
        $signStr = '';
        foreach ($huangdata as $key => $value){
          $signStr .= $key . '=' . $value;
        }
        $signStr .= 'key=' . $merchantKey;
        $sign = strtolower(md5($signStr));
        
        if ($sign !== $post['sign']) {
            
           // 失败， 失败逻辑
            echo 'fail';
            exit;
        } else {
            
        	    $shijian = date('Y-m-d H:i:s' , time());
                $out_trade_no = $huangdata['merchantOrderCode']; //
                $weixin       = $huangdata['platformOrderCode']; //微信订单号
                $cymoney = $huangdata['amount']/100;
                $orderNo = $out_trade_no; //我提交的订单号
                // $openid  = $notify->data["openid"];//微信openid
                
                $order = Cache::store('redis')->get($orderNo);
        		
        		
        		if($order['money'] != $cymoney && $cymoney < 12){
        		   	  echo 'fail';
                      exit;
        		}
        		
        // 		$cymoney = $cymoney * 0.97;
                
                // $order = Db::name("order")->where(['orderid' => $orderNo])->field("userid,zyid,ip,uopenid,orderid")->find();
                
                $userid = $order['userid'];
                
                
                $zyid = $order['zyid'];
                $ip = $order['ip'];
                $ddh = $orderNo;
                $qudao = $ddh;
                $ipz = Db::name("ip")->where(["ddh2" => $ddh])->find();
                if (!empty($ipz))
                {
                    exit;
                }
              
                    $kl = Db::name("kou")->where(['userid' => $userid])->find();
                    $ns = $kl['ns'];
                    if (!empty($kl))
                    {
                        $cs = $kl['cs'] - 1;
                        if ($cs <= 0)
                        {
                            
                            $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                             
                            // $shipin = Db::name("siyou")->where(['id' => $zyid])->find();
                            $zymc = $shipin['name'];
                            $user = Db::name("member")->where(['id' => $userid])->find();
                            $name = $user['nickname'];
                            $data = [];
                            $data['name'] = $name;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh;
                            $data['qudao'] = $qudao;
                            $data['money'] = $cymoney;
                            $data['shijian'] = $shijian;
                            $data['zymc'] = $zymc;
                            $data['type'] = $order['type'];
                            Db::name("kl")->insert($data);
                            Db::name("kou")->where(['userid' => $userid])->update(['cs' => $ns]);
                            // 不扣量
                            // Db::name("siyou")->where(['id'=>$zyid])->setInc("cs");
                            //  Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                            // //
                            $ddh2 = substr($ddh, 0, 10);
                            $data = [];
                            $data['ip'] = $ip;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh2;
                            $data['zyid'] = $zyid;
                            $data['ddh2'] = $ddh;
                            $data['zt'] = "已付";
                            $data['shijian'] = time();
                            $data['openid']  = $order['uopenid'];
                            $data['type'] = $order['type'];
                           
                            $data['uopenid']  = $order['uopenid'];
                           
                           
                            Db::name("ip")->insert($data);
                        }
                        else
                        {
                            Db::name("kou")->where(['userid' => $userid])->setDec("cs"); 
                            // 写自己的数据库
                            $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                            if (empty($row))
                            { 
                                // /更新会员金额
                                $user = Db::name("member")->where(['id' => $userid])->find();
                                if (!empty($user))
                                {
                                    if ($cymoney < 1)
                                    {
                                        $money2 = $cymoney;
                                    }
                                    else
                                    {
                                        $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                                    }
                                    $money = $user['money'] + $money2;
                                    Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                                }
                                
                            
                                $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                                $zymc = $shipin['name'];
                                // if (!empty($shipin))
                                // {
                                //     // Db::name("siyou")->where(['id' => $zyid])->setInc("cs");
                                //  Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                                // }
                                $data = [];
                                $data['zyid'] = $zyid;
                                $data['userid'] = $userid;
                                $data['zymc'] = $zymc;
                                $data['ddh'] = $ddh;
                                $data['money'] = $cymoney;
                                $data['shijian'] = $shijian;
                                $data['wxddh'] = $weixin;
                                $data['type'] = $order['type'];
                                Db::name("dingdan")->insert($data);
                                $ddh2 = substr($ddh, 0, 10);
                                $data = [];
                                $data['ip'] = $ip;
                                $data['userid'] = $userid;
                                $data['ddh'] = $ddh2;
                                $data['zyid'] = $zyid;
                                $data['ddh2'] = $ddh;
                                $data['zt'] = "已付";
                                $data['shijian'] = time();
                                $data['openid']  = $order['uopenid'];
                                $data['type'] = $order['type'];
                                $data['uopenid']  = $order['uopenid'];
                           
                                Db::name("ip")->insert($data);
                            } 
                            // 写自己的数据库
                        } 
                        // 不扣量
                        // 代理ID没添加扣量参数
                    }
                    else
                    { 
                        // 写自己的数据库
                        $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                        if (empty($row))
                        { 
                            // /更新会员金额
                            $user = Db::name("member")->where(['id' => $userid])->find();
                            if (!empty($user))
                            {
                                if ($cymoney < 1)
                                {
                                    $money2 = $cymoney;
                                }
                                else
                                {
                                    $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                                }
                                $money = $user['money'] + $money2;
                                Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                            }
                            $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                            // $shipin = Db::name("siyou")->where(['id' => $zyid])->find();
                            $zymc = $shipin['name'];
                            // if (!empty($shipin))
                            // {
                            //     // Db::name("siyou")->where(['id' => $zyid])->setInc("cs");
                            // Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                            // }
                            $data = [];
                            $data['zyid'] = $zyid;
                            $data['userid'] = $userid;
                            $data['zymc'] = $zymc;
                            $data['ddh'] = $ddh;
                            $data['money'] = $cymoney;
                            $data['shijian'] = $shijian;
                            $data['wxddh'] = $weixin;
                            $data['type'] = $order['type'];
                            Db::name("dingdan")->insert($data);
                            $ddh2 = substr($ddh, 0, 10);
                            $data = [];
                            $data['ip'] = $ip;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh2;
                            $data['zyid'] = $zyid;
                            $data['ddh2'] = $ddh;
                            $data['zt'] = "已付";
                            $data['shijian']  = time();
                            $data['openid']   = $order['uopenid'];
                            $data['uopenid']  = $order['uopenid'];
                            $data['type'] = $order['type'];
                            Db::name("ip")->insert($data);
                        } 
                        // 写自己的数据库
                    } 
          
            // 成功， 成功逻辑
            echo 'success';
        }
     }
    
     
     
     
    public function yunshanfu(){
        
         if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone')||strpos($_SERVER['HTTP_USER_AGENT'], 'iPad')){
              $ip = getIP();
          
        }else if(strpos($_SERVER['HTTP_USER_AGENT'], 'Android')){
              $ip = explode(',', getIP())[0];
        
        }else{
              $ip = getIP();
        }
       
        $ubodingdan = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES)); //提交订单号 . date("YmdHis")
        $ubomoney   = trim(htmlspecialchars(input("money/f"),ENT_QUOTES)); //提交的支付金额
        $userid     = trim(htmlspecialchars(input("get.userid"),ENT_QUOTES)); //提交的用户ID
        $ddh3       = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES));
        $zyid       = trim(htmlspecialchars(input("zyid/d"),ENT_QUOTES));
        $code       = trim(htmlspecialchars(input("get.code"),ENT_QUOTES));
        $uopenid    = trim(htmlspecialchars(input("get.uopenid",''),ENT_QUOTES));
        $pid        = trim(htmlspecialchars(input("get.pid",''),ENT_QUOTES));
        
       
        $yifutime = time() - intval(24) * 3600;
        
        $ename = Cache::store('redis')->get('ylluodi');
      

        
        if(empty($ubodingdan) || empty($ubomoney) || empty($userid) || empty($ddh3) || empty($zyid) || empty($code) || empty($uopenid)){
           
            
             die('参数错误,请重新进入？');
            
        }
        
         $info = Cache::store('redis')->get($userid);
    
        if($info == 'null' || !$info){
            $info = Db::name('dwz')->where(['ddh' => $ddh3])->find();
        }
        
         $jiami_id = $userid;
        
         $userid = $info['userid'];
         
         
        $liebiao = Cache::store('redis')->has('ylgongyou'.$zyid);

		if(!$liebiao || $liebiao === "null"){
		      $liebiao = Db::name("gongyou")->where(['id' => $zyid])->find();
		      if(empty($liebiao)){
		           $liebiao = Db::name("gongyou_copy1")->where(['id' => $zyid])->find();
		      }
		      Cache::store('redis')->set('ylgongyou'.$zyid,json_encode($liebiao),7879680);
		}
         
         
       
        
        $member = Cache::store('redis')->get("ylmember".$userid);
        
      
       
        $money = Db::name('pian')->where(['userid' => $userid])->value('money');
        
        
        
         
         
         if($money != $ubomoney){
            
              die('参数错误,请重新进入？');
         }
         
        //  if($userid == 10027){
             
        //  }else{
        //      $ubomoney = ceil($ubomoney); 
        //  }
         
        
        
       
        
        
        
        
        $ubodingdannew = $ubodingdan . random(4) . $userid . date("dHis");
       
        //$ubodingdannew = $ubodingdan . random(4) . $userid . date("YmdHis");
        $order = Db::name("order")->where(['orderid' => $ubodingdannew])->field('orderid,id')->find();
        
      
       
        if (empty($order))
        {
            $data = [];
            $data['orderid'] = $ubodingdannew;
            $data['ip'] = $ip;
            $data['userid'] = $userid;
            $data['zyid'] = $zyid;
            $data['shijian'] = time();
            $data['money'] = $ubomoney;
            $data['uopenid'] = $uopenid;
            $data['code']    = $code . '|' . $ubodingdan;
            $data['ddh3']    = $ddh3; 
            $data['type']    = 4; 
            $data['jiami']    = $jiami_id; 
            
            Cache::store('redis')->set($ubodingdannew,json_encode($data),86400);
            
            Db::name("order")->insert($data);
       
        }
        
      
            
        $notify_url = "http://".$ename."/index/pay/yunnotify";
        
        $return_url = "http://".$ename."/index/index/shipinok?ddh=" . $ubodingdannew;
        
        $quurl  = "http://zn.dghx139.com/submit";
        $appKey = "4AF2B1B4C5395A167E400D4064A5FB7D"; 
        $options=[
                'pid'=>'1933808520',
            	'type'=>'321',
            	'out_trade_no'=>$ubodingdannew,
            	'notify_url'=>$notify_url,
            	'return_url'=>$return_url,
            	'name'=>'点击返回商家服务【允许】即可观看.',
            	'money'=>$ubomoney,
            	'sitename'=>''
        	];
        
        $options['sign']=$this->yungetsign($appKey,$options);
        $options['sign_type']='MD5';
        
        $url = $quurl.'?pid='.$options['pid'].'&type='.$options['type'].'&out_trade_no='.$options['out_trade_no'].'&notify_url='.$options['notify_url'].'&return_url='.$options['return_url'].'&name='.$options['name'].'&money='.$options['money'].'&sitename='.$options['sitename'].'&sign='.$options['sign'].'&sign_type=MD5';
        
      
       $this->redirect($url);


    }
    
    
    public function yungetsign($appKey,$param){
    	$signPars = "";
    	ksort($param);
    	foreach ($param as $k => $v) {
    		if ($k!="sign"&&$v!="") {
    			$signPars .= $k . "=" . $v . "&";
    		}
    	}
    	$signPars = rtrim($signPars, '&');
    	$signPars .= $appKey;
    	$sign = md5($signPars);
    	return $sign;
}


    public function  yunnotify(){
        
        
        $options = $_GET;
         
    	$oldSign = $options['sign'];
    	unset($options['sign']);
        unset($options['sign_type']);
        $appKey = "4AF2B1B4C5395A167E400D4064A5FB7D"; 
    	$sign   = $this->yungetsign($appKey,$options);
    	
    	$trade_no     = $options['trade_no'];//永生订单号
    	$out_trade_no = $options['out_trade_no'];//系统订单号
        $money        = $options['money'];//订单金额
    	if($sign ==$oldSign) {
    	    
        	    $shijian = date('Y-m-d H:i:s' , time());
                $out_trade_no = $out_trade_no; //
                $weixin       = $trade_no; //微信订单号
                $cymoney = $money;
                $orderNo = $out_trade_no; //我提交的订单号
                // $openid  = $notify->data["openid"];//微信openid
                
                $order = Cache::store('redis')->get($orderNo);
        		
        		
        		if($order['money'] != $cymoney && $cymoney < 12){
        		   	 exit("签名失败");
        		}
        		
        // 		$cymoney = $cymoney * 0.97;
                
                // $order = Db::name("order")->where(['orderid' => $orderNo])->field("userid,zyid,ip,uopenid,orderid")->find();
                
                $userid = $order['userid'];
                
                
                $zyid = $order['zyid'];
                $ip = $order['ip'];
                $ddh = $orderNo;
                $qudao = $ddh;
                $ipz = Db::name("ip")->where(["ddh2" => $ddh])->find();
                if (!empty($ipz))
                {
                    exit;
                }
              
                    $kl = Db::name("kou")->where(['userid' => $userid])->find();
                    $ns = $kl['ns'];
                    if (!empty($kl))
                    {
                        $cs = $kl['cs'] - 1;
                        if ($cs <= 0)
                        {
                            
                            $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                             
                            // $shipin = Db::name("siyou")->where(['id' => $zyid])->find();
                            $zymc = $shipin['name'];
                            $user = Db::name("member")->where(['id' => $userid])->find();
                            $name = $user['nickname'];
                            $data = [];
                            $data['name'] = $name;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh;
                            $data['qudao'] = $qudao;
                            $data['money'] = $cymoney;
                            $data['shijian'] = $shijian;
                            $data['zymc'] = $zymc;
                            $data['type'] = $order['type'];
                            Db::name("kl")->insert($data);
                            Db::name("kou")->where(['userid' => $userid])->update(['cs' => $ns]);
                            // 不扣量
                            // Db::name("siyou")->where(['id'=>$zyid])->setInc("cs");
                            //  Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                            // //
                            $ddh2 = substr($ddh, 0, 10);
                            $data = [];
                            $data['ip'] = $ip;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh2;
                            $data['zyid'] = $zyid;
                            $data['ddh2'] = $ddh;
                            $data['zt'] = "已付";
                            $data['shijian'] = time();
                            $data['openid']  = $order['uopenid'];
                            $data['type']    = $order['type'];
                           
                            $data['uopenid']  = $order['uopenid'];
                           
                           
                            Db::name("ip")->insert($data);
                        }
                        else
                        {
                            Db::name("kou")->where(['userid' => $userid])->setDec("cs"); 
                            // 写自己的数据库
                            $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                            if (empty($row))
                            { 
                                // /更新会员金额
                                $user = Db::name("member")->where(['id' => $userid])->find();
                                if (!empty($user))
                                {
                                    if ($cymoney < 1)
                                    {
                                        $money2 = $cymoney;
                                    }
                                    else
                                    {
                                        $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                                    }
                                    $money = $user['money'] + $money2;
                                    Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                                }
                                
                            
                                $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                                $zymc = $shipin['name'];
                                // if (!empty($shipin))
                                // {
                                //     // Db::name("siyou")->where(['id' => $zyid])->setInc("cs");
                                //  Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                                // }
                                $data = [];
                                $data['zyid'] = $zyid;
                                $data['userid'] = $userid;
                                $data['zymc'] = $zymc;
                                $data['ddh'] = $ddh;
                                $data['money'] = $cymoney;
                                $data['shijian'] = $shijian;
                                $data['wxddh'] = $weixin;
                                $data['type']    = $order['type'];
                                Db::name("dingdan")->insert($data);
                                $ddh2 = substr($ddh, 0, 10);
                                $data = [];
                                $data['ip'] = $ip;
                                $data['userid'] = $userid;
                                $data['ddh'] = $ddh2;
                                $data['zyid'] = $zyid;
                                $data['ddh2'] = $ddh;
                                $data['zt'] = "已付";
                                $data['shijian'] = time();
                                $data['openid']  = $order['uopenid'];
                                $data['type']    = $order['type'];
                                $data['uopenid']  = $order['uopenid'];
                           
                                Db::name("ip")->insert($data);
                            } 
                            // 写自己的数据库
                        } 
                        // 不扣量
                        // 代理ID没添加扣量参数
                    }
                    else
                    { 
                        // 写自己的数据库
                        $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                        if (empty($row))
                        { 
                            // /更新会员金额
                            $user = Db::name("member")->where(['id' => $userid])->find();
                            if (!empty($user))
                            {
                                if ($cymoney < 1)
                                {
                                    $money2 = $cymoney;
                                }
                                else
                                {
                                    $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                                }
                                $money = $user['money'] + $money2;
                                Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                            }
                            $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                            // $shipin = Db::name("siyou")->where(['id' => $zyid])->find();
                            $zymc = $shipin['name'];
                            // if (!empty($shipin))
                            // {
                            //     // Db::name("siyou")->where(['id' => $zyid])->setInc("cs");
                            // Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                            // }
                            $data = [];
                            $data['zyid'] = $zyid;
                            $data['userid'] = $userid;
                            $data['zymc'] = $zymc;
                            $data['ddh'] = $ddh;
                            $data['money'] = $cymoney;
                            $data['shijian'] = $shijian;
                            $data['wxddh'] = $weixin;
                            $data['type']    = $order['type'];
                            Db::name("dingdan")->insert($data);
                            $ddh2 = substr($ddh, 0, 10);
                            $data = [];
                            $data['ip'] = $ip;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh2;
                            $data['zyid'] = $zyid;
                            $data['ddh2'] = $ddh;
                            $data['zt'] = "已付";
                            $data['shijian']  = time();
                            $data['openid']   = $order['uopenid'];
                            $data['uopenid']  = $order['uopenid'];
                            $data['type']     = $order['type'];
                            Db::name("ip")->insert($data);
                        } 
                        // 写自己的数据库
                    } 
          
    		
    		echo 'success';
    	}
		
        
    }
    
    
       //酷爱发起支付 
    public function kuaipay(){
        
        if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone')||strpos($_SERVER['HTTP_USER_AGENT'], 'iPad')){
               $ip = getIP();
               $shouji = 1;
          
        }else if(strpos($_SERVER['HTTP_USER_AGENT'], 'Android')){
               $ip = explode(',', getIP())[0];
               $shouji = 0;
        
        }else{
               $ip = getIP();
               $shouji = 1;
        }
        
        $ubodingdan = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES)); //提交订单号 . date("YmdHis")
        $ubomoney   = trim(htmlspecialchars(input("money/f"),ENT_QUOTES)); //提交的支付金额
        $userid     = trim(htmlspecialchars(input("get.userid"),ENT_QUOTES)); //提交的用户ID
        $ddh3       = trim(htmlspecialchars(input("ddh"),ENT_QUOTES));
        $zyid       = trim(htmlspecialchars(input("zyid/d"),ENT_QUOTES));
        $code       = trim(htmlspecialchars(input("get.code"),ENT_QUOTES));
        $uopenid    = trim(htmlspecialchars(input("get.uopenid",''),ENT_QUOTES));
        
      
      
        
        $info = Db::name('dwz')->where(['ddh' => $ddh3])->find();
         
        
        $userid = $info['userid'];
        
      
        
        $jiami_id = $info['jiami'];
        
        if(!preg_match("/^\d+(\.\d+)?$/",$ubomoney)){
            
             die('金额错误？');
            
        }
        
        if($ddh3 == "4ER3chCsmg"){
              $this->redirect("https://xw.qq.com/");
        }
        
        $liebiao = Cache::store('redis')->has('ylgongyou'.$zyid);

		if(!$liebiao || $liebiao === "null"){
		      $liebiao = Db::name("gongyou")->where(['id' => $zyid])->find();
		      if(empty($liebiao)){
		           $liebiao = Db::name("gongyou_copy1")->where(['id' => $zyid])->find();
		      }
		      Cache::store('redis')->set('ylgongyou'.$zyid,json_encode($liebiao),7879680);
		}
        
        
        $ubodingdannew = $ubodingdan . random(4) . $userid . date("dHis").mt_rand(1,10);
       
         
        //微信内服开关
       
       
        //$ubodingdannew = $ubodingdan . random(4) . $userid . date("YmdHis");
        $order   = Db::name("order")->where(['orderid' => $ubodingdannew])->field('orderid,id')->find();
        
       
        
       
       
       $member = Cache::store('redis')->get("ylmember".$userid);
    
       $zftd = $member['zftd'];
        
        
        
       
        if (empty($order))
        {
            $data = [];
            $data['orderid']  = $ubodingdannew;
            $data['ip']       = $ip;
            $data['userid']   = $userid;
            $data['zyid']     = $zyid;
            $data['shijian']  = time();
            $data['money']    = $ubomoney;
            $data['uopenid']  = $uopenid;
            $data['code']     = $code . '|' . $ubodingdan;
            $data['ddh3']     = $ddh3; 
            $data['zftd']     = $zftd;
            $data['type']     = 6;//酷爱支付
            $data['jiami']    = $jiami_id;
            $data['shouji']   = $shouji;
            
            Cache::store('redis')->set($ubodingdannew,json_encode($data),86400);
            
            Db::name("order")->insert($data);
       
        }
        
        $ubomoney = $ubomoney * 100;
       // $ubomoney = 200;
        
       $ename = Cache::store('redis')->get('ylluodi');
        
        
        $notify_url = "http://".$ename."/index/pay/kuaihuidiao";
        
        $return_url = "http://".$ename."/index/index/shipinok?ddh=" . $ubodingdannew;
     
        $paydata = array(
            'mchId'=>"700500547",
            'billNo'=>$ubodingdannew,
            'totalAmount'=>$ubomoney,
            'billDesc'=>'永顺在线支付',
            'way'=>'qrcode',
            'payment'=>'wechat',
            'notifyUrl'=>$notify_url,
            'returnUrl'=>$return_url,
            'attach'=>""
        );
        $Md5key = "8e2a3625e015691ca7bbd597161edc9b142cf0e6f86f7cae72a7e00d460e6cdc";
        $paydata['sign'] = $this->markSign($paydata,$Md5key);
        $paydata['debug'] = true;
        $url = "http://pay.maihekejixcx.com/game/unifiedorder";
        $ret= $this->curl($url,json_encode($paydata));
        $data = json_decode($ret,true);
     
        if($data['code'] == 0){
          
            //json里有两个连接payurl 真实支付地址  和linkurl 走插件连接   这两个看需要自行取值
            $url = $data['result']['linkUrl'];
            header("Location:{$url}");
        }else{
            exit($data['msg']);
        }
    }

    //酷爱curl请求
    public function curl($url,$post_data){
        $ch = curl_init();
        $header = [
            'Content-Type: application/json',
            'Content-Length: '.strlen($post_data)
        ];
        curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        // 我们在POST数据哦！
        curl_setopt($ch, CURLOPT_POST, 1);
        // 把post的变量加上
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }
    //酷爱签名
    public function markSign($paydata,$signkey){
        ksort($paydata);
        $str='';
        foreach($paydata as $k=>$v){
            if($k != "sign" && $v!= ""){
                $str.=$k."=".$v."&";
            }
        }
        return strtoupper(md5($str."key=".$signkey));
    }

    //酷爱回调
    public function kuaihuidiao(){
        //回调验证方法
        $notifydata = $_POST;
        $sign       = $notifydata['sign'];
        $Md5key     = "8e2a3625e015691ca7bbd597161edc9b142cf0e6f86f7cae72a7e00d460e6cdc";
        $mysign     = $this->markSign($notifydata,$Md5key);
        if($mysign == $sign){
    
        	   $out_trade_no  = $notifydata['billNo'];
        
               $trade_no      = $notifydata['tradeNo'];
        
        
        	    $trade_no     = $trade_no;//彩虹订单号
            	$out_trade_no = $out_trade_no;//系统订单号
                $money        = $notifydata['totalAmount'];//订单金额
    
    	    
        	    $shijian = date('Y-m-d H:i:s' , time());
                $out_trade_no = $out_trade_no; //
                $weixin       = $trade_no; //微信订单号
                $cymoney = $money;
                $orderNo = $out_trade_no; //我提交的订单号
                // $openid  = $notify->data["openid"];//微信openid
                
                $order = Cache::store('redis')->get($orderNo);
        		
        		
        		if($order['money'] != $cymoney && $cymoney < 12){
        		   	 exit("签名失败");
        		}
        		
        // 		$cymoney = $cymoney * 0.97;
                
                // $order = Db::name("order")->where(['orderid' => $orderNo])->field("userid,zyid,ip,uopenid,orderid")->find();
                
                $userid = $order['userid'];
                
                
                $zyid = $order['zyid'];
                $ip = $order['ip'];
                $ddh = $orderNo;
                $qudao = $ddh;
                $ipz = Db::name("ip")->where(["ddh2" => $ddh])->find();
                if (!empty($ipz))
                {
                    exit;
                }
              
                    $kl = Db::name("kou")->where(['userid' => $userid])->find();
                    $ns = $kl['ns'];
                    if (!empty($kl))
                    {
                        $cs = $kl['cs'] - 1;
                        if ($cs <= 0)
                        {
                            
                            $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                             
                            // $shipin = Db::name("siyou")->where(['id' => $zyid])->find();
                            $zymc = $shipin['name'];
                            $user = Db::name("member")->where(['id' => $userid])->find();
                            $name = $user['nickname'];
                            $data = [];
                            $data['name'] = $name;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh;
                            $data['qudao'] = $qudao;
                            $data['money'] = $cymoney;
                            $data['shijian'] = $shijian;
                            $data['zymc'] = $zymc;
                            $data['type'] = $order['type'];
                            Db::name("kl")->insert($data);
                            Db::name("kou")->where(['userid' => $userid])->update(['cs' => $ns]);
                            // 不扣量
                            // Db::name("siyou")->where(['id'=>$zyid])->setInc("cs");
                             Db::name("gongyou")->where(['id' => $zyid])->setInc("count");
                            // //
                            $ddh2 = substr($ddh, 0, 10);
                            $data = [];
                            $data['ip'] = $ip;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh2;
                            $data['zyid'] = $zyid;
                            $data['ddh2'] = $ddh;
                            $data['zt'] = "已付";
                            $data['shijian'] = time();
                            $data['openid']  = $order['uopenid'];
                            $data['type']    = $order['type'];
                           
                            $data['uopenid']  = $order['uopenid'];
                           
                           
                            Db::name("ip")->insert($data);
                        }
                        else
                        {
                            Db::name("kou")->where(['userid' => $userid])->setDec("cs"); 
                            // 写自己的数据库
                            $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                            if (empty($row))
                            { 
                                // /更新会员金额
                                $user = Db::name("member")->where(['id' => $userid])->find();
                                if (!empty($user))
                                {
                                    if ($cymoney < 1)
                                    {
                                        $money2 = $cymoney;
                                    }
                                    else
                                    {
                                        $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                                    }
                                    $money = $user['money'] + $money2;
                                    Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                                }
                                
                            
                                $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                                $zymc = $shipin['name'];
                                // if (!empty($shipin))
                                // {
                                //     // Db::name("siyou")->where(['id' => $zyid])->setInc("cs");
                                //  Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                                // }
                                $data = [];
                                $data['zyid'] = $zyid;
                                $data['userid'] = $userid;
                                $data['zymc'] = $zymc;
                                $data['ddh'] = $ddh;
                                $data['money'] = $cymoney;
                                $data['shijian'] = $shijian;
                                $data['wxddh'] = $weixin;
                                $data['type']    = $order['type'];
                                Db::name("dingdan")->insert($data);
                                $ddh2 = substr($ddh, 0, 10);
                                $data = [];
                                $data['ip'] = $ip;
                                $data['userid'] = $userid;
                                $data['ddh'] = $ddh2;
                                $data['zyid'] = $zyid;
                                $data['ddh2'] = $ddh;
                                $data['zt'] = "已付";
                                $data['shijian'] = time();
                                $data['openid']  = $order['uopenid'];
                                $data['type']    = $order['type'];
                                $data['uopenid']  = $order['uopenid'];
                           
                                Db::name("ip")->insert($data);
                            } 
                            // 写自己的数据库
                        } 
                        // 不扣量
                        // 代理ID没添加扣量参数
                    }
                    else
                    { 
                        // 写自己的数据库
                        $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                        if (empty($row))
                        { 
                            // /更新会员金额
                            $user = Db::name("member")->where(['id' => $userid])->find();
                            if (!empty($user))
                            {
                                if ($cymoney < 1)
                                {
                                    $money2 = $cymoney;
                                }
                                else
                                {
                                    $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                                }
                                $money = $user['money'] + $money2;
                                Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                            }
                            $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                            // $shipin = Db::name("siyou")->where(['id' => $zyid])->find();
                            $zymc = $shipin['name'];
                            
                            Db::name("gongyou")->where(['id' => $zyid])->setInc("count");
                            $data = [];
                            $data['zyid'] = $zyid;
                            $data['userid'] = $userid;
                            $data['zymc'] = $zymc;
                            $data['ddh'] = $ddh;
                            $data['money'] = $cymoney;
                            $data['shijian'] = $shijian;
                            $data['wxddh'] = $weixin;
                            $data['type']    = $order['type'];
                            Db::name("dingdan")->insert($data);
                            $ddh2 = substr($ddh, 0, 10);
                            $data = [];
                            $data['ip'] = $ip;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh2;
                            $data['zyid'] = $zyid;
                            $data['ddh2'] = $ddh;
                            $data['zt'] = "已付";
                            $data['shijian']  = time();
                            $data['openid']   = $order['uopenid'];
                            $data['uopenid']  = $order['uopenid'];
                            $data['type']     = $order['type'];
                            Db::name("ip")->insert($data);
                        } 
                        // 写自己的数据库
                    } 
        	    
                //验证成功
                //执行您的业务逻辑
                    echo 'SUCCESS';
        }else{
            //签名失败
                echo 'signError';
        }
    }
    
    public function newpay(){
        
        if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone')||strpos($_SERVER['HTTP_USER_AGENT'], 'iPad')){
               $ip = getIP();
               $shouji = 1;
          
        }else if(strpos($_SERVER['HTTP_USER_AGENT'], 'Android')){
               $ip = explode(',', getIP())[0];
               $shouji = 0;
        
        }else{
               $ip = getIP();
               $shouji = 1;
        }
        $ubodingdan = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES)); //提交订单号 . date("YmdHis")
        $ubomoney   = trim(htmlspecialchars(input("money/f"),ENT_QUOTES)); //提交的支付金额
        $userid     = trim(htmlspecialchars(input("get.userid"),ENT_QUOTES)); //提交的用户ID
        $ddh3       = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES));
        $zyid       = trim(htmlspecialchars(input("zyid/d"),ENT_QUOTES));
        $code       = trim(htmlspecialchars(input("get.code"),ENT_QUOTES));
        $uopenid    = trim(htmlspecialchars(input("get.uopenid",''),ENT_QUOTES));
        $pid        = trim(htmlspecialchars(input("get.pid",''),ENT_QUOTES));
        
        $yifutime = time() - intval(24) * 3600;
        
        $ename = Cache::store('redis')->get('ylluodi');
      
        if(empty($ubodingdan) || empty($ubomoney) || empty($userid) || empty($ddh3) || empty($zyid) || empty($code) || empty($uopenid)){
            die('参数错误,请重新进入？');
        }
        $info = Cache::store('redis')->get($userid);
    
        if($info == 'null' || !$info){
            $info = Db::name('dwz')->where(['ddh' => $ddh3])->find();
        }
        
        $jiami_id = $userid;
        
        $userid = $info['userid'];
         session('useridopenid', $uopenid);
        $liebiao = Cache::store('redis')->has('ylgongyou'.$zyid);

		if(!$liebiao || $liebiao === "null"){
		      $liebiao = Db::name("gongyou")->where(['id' => $zyid])->find();
		      if(empty($liebiao)){
		           $liebiao = Db::name("gongyou_copy1")->where(['id' => $zyid])->find();
		      }
		      Cache::store('redis')->set('ylgongyou'.$zyid,json_encode($liebiao),7879680);
		}
        $member = Cache::store('redis')->get("member".$userid);
        
        $money = Db::name('pian')->where(['userid' => $userid])->value('money');
        
        if($money != $ubomoney){
            die('参数错误,请重新进入？');
        }
        
        $ubodingdannew = $ubodingdan . random(4) . $userid . date("dHis");
        $order = Db::name("order")->where(['orderid' => $ubodingdannew])->field('orderid,id')->find();
        
        if (empty($order))
        {
            $data = [];
            $data['orderid'] = $ubodingdannew;
            $data['ip'] = $ip;
            $data['userid'] = $userid;
            $data['zyid'] = $zyid;
            $data['shijian'] = time();
            $data['money'] = $ubomoney;
            $data['uopenid'] = $uopenid;
            $data['code']    = $code . '|' . $ubodingdan;
            $data['ddh3']    = $ddh3; 
            $data['type']    = 4; //狂风支付
            $data['jiami']    = $jiami_id; 
            $data['shouji']   = $shouji;
            Cache::store('redis')->set($ubodingdannew,json_encode($data),86400);
            
            Db::name("order")->insert($data);
       
        }

        /**************************请求参数**************************/
        //回调地址
        $notify_url = "http://".$ename."/index/pay/newpayhuitiao";
        //异步通知地址
        $return_url = "http://".$ename."/index/index/shipinok?ddh=" . $ubodingdannew;
        //商户订单号
        $out_trade_no = $ubodingdannew;
        //商户网站订单系统中唯一订单号，必填
        
        //商品名称
        $name    = "测试商品";
		//付款金额
        $money   = $ubomoney;
		//站点名称
        $sitename = '狂风新测试站点';
        $url = $this->newmp_pay(53, "4f91062555bc320885c4965c8b07ba2d",$ubodingdannew,$name, $money, $notify_url, $return_url, $mchid = '');
        if (preg_match('/^http(s)?:\/\/*/i', $url) === 1) {
            header("Location: $url");
        } else {
            echo $url;
        }
        
    }
    
    //新狂风回调
    public function  newpayhuitiao(){
        $token = '4f91062555bc320885c4965c8b07ba2d';
        $data['id'] = $_REQUEST['id']; // 对接的id
        $data['trade_no'] = $_REQUEST['trade_no']; // 微信订单号
        $data['out_trade_no'] = $_REQUEST['out_trade_no']; // 订单号
        $data['name'] = $_REQUEST['name'];
        $data['money'] = $_REQUEST['money']; // 支付金额
        $data['trade_status'] = $_REQUEST['trade_status']; // TRADE_SUCCESS or TRADE_FAIL
        $data = get_magic_quotes_gpc() ? stripslashes(array_filter($data)) : array_filter($data);
        ksort($data);
    	$str1 = '';
        foreach ($data as $k => $v) {
            $str1 .= '&' . $k . "=" . $v;
        }
        $sign = md5(trim($str1 . $token, '&'));
        $oldSign = $_REQUEST['sign'];
    	if($sign ==$oldSign) {
        	    $shijian      = date('Y-m-d H:i:s' , time());
                $out_trade_no = $data['out_trade_no']; //
                $weixin       = $data['trade_no']; //微信订单号
                $cymoney      = $data['money'];
                $orderNo      = $out_trade_no; //我提交的订单号
                // $openid  = $notify->data["openid"];//微信openid
                
                $order = Cache::store('redis')->get($orderNo);
        		
        		
        		if($order['money'] != $cymoney && $cymoney > 10){
        		   	 exit("签名失败");
        		}
        		
        // 		$cymoney = $cymoney * 0.97;
                
                // $order = Db::name("order")->where(['orderid' => $orderNo])->field("userid,zyid,ip,uopenid,orderid")->find();
                
                $userid = $order['userid'];
                
                
                $zyid = $order['zyid'];
                $ip = $order['ip'];
                $ddh = $orderNo;
                $qudao = $ddh;
                $ipz = Db::name("ip")->where(["ddh2" => $ddh])->find();
                if (!empty($ipz))
                {
                    exit;
                }
              
                    $kl = Db::name("kou")->where(['userid' => $userid])->find();
                    $ns = $kl['ns'];
                    if (!empty($kl))
                    {
                        $cs = $kl['cs'] - 1;
                        if ($cs <= 0)
                        {
                            
                            $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                             
                            // $shipin = Db::name("siyou")->where(['id' => $zyid])->find();
                            $zymc = $shipin['name'];
                            $user = Db::name("member")->where(['id' => $userid])->find();
                            $name = $user['nickname'];
                            $data = [];
                            $data['name'] = $name;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh;
                            $data['qudao'] = $qudao;
                            $data['money'] = $cymoney;
                            $data['shijian'] = $shijian;
                            $data['zymc'] = $zymc;
                            $data['type'] = $order['type'];
                            Db::name("kl")->insert($data);
                            Db::name("kou")->where(['userid' => $userid])->update(['cs' => $ns]);
                            // 不扣量
                            // Db::name("siyou")->where(['id'=>$zyid])->setInc("cs");
                            //  Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                            // //
                            $ddh2 = substr($ddh, 0, 10);
                            $data = [];
                            $data['ip'] = $ip;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh2;
                            $data['zyid'] = $zyid;
                            $data['ddh2'] = $ddh;
                            $data['zt'] = "已付";
                            $data['shijian'] = time();
                            $data['openid']  = $order['uopenid'];
                            $data['type']    = $order['type'];
                           
                            $data['uopenid']  = $order['uopenid'];
                           
                           
                            Db::name("ip")->insert($data);
                        }
                        else
                        {
                            Db::name("kou")->where(['userid' => $userid])->setDec("cs"); 
                            // 写自己的数据库
                            $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                            if (empty($row))
                            { 
                                // /更新会员金额
                                $user = Db::name("member")->where(['id' => $userid])->find();
                                if (!empty($user))
                                {
                                    if ($cymoney < 1)
                                    {
                                        $money2 = $cymoney;
                                    }
                                    else
                                    {
                                        $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                                    }
                                    $money = $user['money'] + $money2;
                                    Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                                }
                                
                            
                                $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                                $zymc = $shipin['name'];
                                // if (!empty($shipin))
                                // {
                                //     // Db::name("siyou")->where(['id' => $zyid])->setInc("cs");
                                //  Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                                // }
                                $data = [];
                                $data['zyid'] = $zyid;
                                $data['userid'] = $userid;
                                $data['zymc'] = $zymc;
                                $data['ddh'] = $ddh;
                                $data['money'] = $cymoney;
                                $data['shijian'] = $shijian;
                                $data['wxddh'] = $weixin;
                                $data['type']    = $order['type'];
                                Db::name("dingdan")->insert($data);
                                $ddh2 = substr($ddh, 0, 10);
                                $data = [];
                                $data['ip'] = $ip;
                                $data['userid'] = $userid;
                                $data['ddh'] = $ddh2;
                                $data['zyid'] = $zyid;
                                $data['ddh2'] = $ddh;
                                $data['zt'] = "已付";
                                $data['shijian'] = time();
                                $data['openid']  = $order['uopenid'];
                                $data['type']    = $order['type'];
                                $data['uopenid']  = $order['uopenid'];
                           
                                Db::name("ip")->insert($data);
                            } 
                            // 写自己的数据库
                        } 
                        // 不扣量
                        // 代理ID没添加扣量参数
                    }
                    else
                    { 
                        // 写自己的数据库
                        $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                        if (empty($row))
                        { 
                            // /更新会员金额
                            $user = Db::name("member")->where(['id' => $userid])->find();
                            if (!empty($user))
                            {
                                if ($cymoney < 1)
                                {
                                    $money2 = $cymoney;
                                }
                                else
                                {
                                    $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                                }
                                $money = $user['money'] + $money2;
                                Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                            }
                            $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                            // $shipin = Db::name("siyou")->where(['id' => $zyid])->find();
                            $zymc = $shipin['name'];
                            // if (!empty($shipin))
                            // {
                            //     // Db::name("siyou")->where(['id' => $zyid])->setInc("cs");
                            // Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                            // }
                            $data = [];
                            $data['zyid'] = $zyid;
                            $data['userid'] = $userid;
                            $data['zymc'] = $zymc;
                            $data['ddh'] = $ddh;
                            $data['money'] = $cymoney;
                            $data['shijian'] = $shijian;
                            $data['wxddh'] = $weixin;
                            $data['type']    = $order['type'];
                            Db::name("dingdan")->insert($data);
                            $ddh2 = substr($ddh, 0, 10);
                            $data = [];
                            $data['ip'] = $ip;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh2;
                            $data['zyid'] = $zyid;
                            $data['ddh2'] = $ddh;
                            $data['zt'] = "已付";
                            $data['shijian']  = time();
                            $data['openid']   = $order['uopenid'];
                            $data['uopenid']  = $order['uopenid'];
                            $data['type']     = $order['type'];
                            Db::name("ip")->insert($data);
                        } 
                        // 写自己的数据库
                    } 
    		echo 'success';
    	}
    }
    
    /**
     * @Note  发起支付
     * @param @id          对接ID
     * @param @key          对接Token
     * @param @trade_no   订单号
     * @param @name          商品名称
     * @param @money      金额（元）
     * @param @mchid      商户id (如果为空,则随机选择支付商户)
     * @param @notify_url 异步通知地址
     * @param @return_url 同步支付地址（支付成功后跳转的页面）
     * @param @return string  返回支付链接
     */
    public function newmp_pay($id, $key, $trade_no, $name, $money, $notify_url, $return_url)
    {
        $url = 'http://wxgzh.dreambuilder.store/wxrecharge.html';
        $data = [
            'id' => $id,
            'key' => $key,
            'trade_no' => $trade_no,
            'name' => $name,
            'money' => $money,
            'notify_url' => $notify_url,
            'return_url' => $return_url,
        ];
        $data = get_magic_quotes_gpc() ? stripslashes(array_filter($data)) : array_filter($data);
        ksort($data);
        $str1 = '';
        foreach ($data as $k => $v) {
            $str1 .= '&' . $k . "=" . $v;
        }
        $sign = md5(trim($str1 . $key, '&'));
    
        $data['sign'] = $sign;
        $data['sign_type'] = 'MD5';
        $url = $url . '?' . http_build_query($data);
        $resRaw = file_get_contents($url, false, stream_context_create(array(
            'http' => array(
                'ignore_errors' => true
            )
        )));
        $res = json_decode($resRaw, true);
        if (isset($res['url'])) {
            $resUrl = $res['url'];
        } else {
            $resUrl = $resRaw;
        }
        return $resUrl;
    }
    
    
    public function bluePayfj()
    {
        
        if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone')||strpos($_SERVER['HTTP_USER_AGENT'], 'iPad')){
              $ip = getIP();
          
        }else if(strpos($_SERVER['HTTP_USER_AGENT'], 'Android')){
              $ip = explode(',', getIP())[0];
        
        }else{
              $ip = getIP();
        }
       
        $ubodingdan = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES)); //提交订单号 . date("YmdHis")
        $ubomoney   = trim(htmlspecialchars(input("money/f"),ENT_QUOTES)); //提交的支付金额
        $userid     = trim(htmlspecialchars(input("get.userid"),ENT_QUOTES)); //提交的用户ID
        $ddh3       = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES));
        $zyid       = trim(htmlspecialchars(input("zyid/d"),ENT_QUOTES));
        $code       = trim(htmlspecialchars(input("get.code"),ENT_QUOTES));
        $uopenid    = trim(htmlspecialchars(input("get.uopenid",''),ENT_QUOTES));
        $pid        = trim(htmlspecialchars(input("get.pid",''),ENT_QUOTES));
        $is_baotian = trim(htmlspecialchars(input("get.is_baotian"),ENT_QUOTES));
        $b_money    = trim(htmlspecialchars(input("get.b_money"),ENT_QUOTES));
      
       
        $yifutime = time() - intval(24) * 3600;
        
        $ename = Cache::store('redis')->get('ylluodi');
      
        if(empty($ubodingdan) || empty($ubomoney) || empty($userid) || empty($ddh3) || empty($zyid) || empty($code) || empty($uopenid)){
           
            
             die('参数错误,请重新进入？');
            
        }
        
         session('useridopenid', $uopenid);
        
         $info = Db::name('baotian')->where(['uopenid' => $uopenid])->order('id desc')->find();
        
        if($info && $info['end_time'] > time()){
           
            $geturl = "http://".$ename."/index/index/shipinok.html?zyid=" . $info['zyid'] ."&ddh=".$info['orderid']."&uopenid=".$uopenid."&bzyid=".$zyid;
            
          
            $this->redirect($geturl);
        }
        
         $info = Cache::store('redis')->get($userid);
    
        if($info == 'null' || !$info){
            $info = Db::name('dwz')->where(['ddh' => $ddh3])->find();
        }
        
         $jiami_id = $userid;
        
         $userid = $info['userid'];
         
        
        $liebiao = Cache::store('redis')->has('ylgongyou'.$zyid);

		if(!$liebiao || $liebiao === "null"){
		      $liebiao = Db::name("gongyou")->where(['id' => $zyid])->find();
		     
		      Cache::store('redis')->set('ylgongyou'.$zyid,json_encode($liebiao),7879680);
		}
         
        $member = Db::name('member')->where(['id' => $userid])->find();
         
       
         
       
        if($b_money >= 30 && $is_baotian == 1){
            
             
              $order_type = 2;
              $money      = Db::name('pian')->where(['userid' => $userid])->value('b_money');
             
              if($money != $b_money){
                 die('参数错误');
              }
             
              $ubomoney = $money;
             
           
            //   $longurl = "http://".$ename ."/index/fang/index.html?userid=" . $jiami_id . '&zyid=' . $zyid . "&code=" . $jiami_id."&uopenid=".$uopenid. '&money=' . $ubomoney."&ddh=".$ddh3."&is_baotian=1"."&b_money=".$b_money;
            //   $this->redirect($longurl);
            
             
        }else{
             $order_type = 1;
             $ubomoney = Db::name('pian')->where(['userid' => $userid])->value('money');
            
            
        }
        
   
            
            
      
        
       
        
        
        
        
        $ubodingdannew = $ubodingdan . random(4) . $userid . date("dHis");
       
        //$ubodingdannew = $ubodingdan . random(4) . $userid . date("YmdHis");
        $order = Db::name("order")->where(['orderid' => $ubodingdannew])->field('orderid,id')->find();
        
      
       
        if (empty($order))
        {
            $data = [];
            $data['orderid'] = $ubodingdannew;
            $data['ip'] = $ip;
            $data['userid'] = $userid;
            $data['zyid'] = $zyid;
            $data['shijian'] = time();
            $data['money'] = $ubomoney;
            $data['uopenid'] = $uopenid;
            $data['code']    = $code . '|' . $ubodingdan;
            $data['ddh3']    = $ddh3; 
            $data['type']    = 3; //蓝帽支付
            $data['jiami']    = $jiami_id; 
            $data['order_type']    = $order_type;
            
            Cache::store('redis')->set($ubodingdannew,json_encode($data),86400);
            
            Db::name("order")->insert($data);
       
        }
        
        $notify_url = "http://".$ename."/index/pay/bluePayfjhuitiao";
        
        $return_url = "http://".$ename."/index/index/shipinok?ddh=" . $ubodingdannew;
        
        //商户ID
    	$mchId = '10142';
    	//商户秘钥
    	$mchKey = 'AQWBWJOQHDVOWHMM5B1EMEYMFAXPCQNPXSBEA3A01DCJLKJ2FPMXA3ETO68ZIRBQ2Z1USHTE0N222UA5CBOPUTBQHXP3DEDR9A8BRAXZ86KPDU19YO4RG7KB0GHFFI0J';
    	//商户应用ID
    	$appId = '1c0a00b816da47349d5893010846f2c0';
    	//支付产品ID
    	$productId= '8000';
    	//支付网关地址
    	$payHost = 'http://pay.chpal.club';
        
        $amount =  $ubomoney * 100; //元转换为分
        $paramArray = array(
    		"mchId" => $mchId, //商户ID
    // 		"appId" => $appId,  //商户应用ID
    		"productId" => $productId,  //支付产品ID
    		"mchOrderNo" => $ubodingdannew,// 商户订单号
    // 		"currency" => 'cny',//币种
    		"amount" => $amount . "", //支付金额,单位分
    		//"clientIp" => '210.73.10.148',   //客户端IP
    		//"device" => 'ios10.3.1',    //客户端设备
    		"returnUrl" => $return_url,	 //支付结果前端跳转URL
    		"notifyUrl" => $notify_url,	 //支付结果后台回调URL
    // 		"subject" => '网络购物',	 //商品主题
    // 		"body" => '网络购物',	 //商品描述信息
    		"param1" => '',	 //扩展参数1
    		//"param2" =>  '',	 //扩展参数2
    // 		"extra" =>  '附加参数'	 //附加参数
        );
       
    	$sign = paramArraySign($paramArray, $mchKey);  //签名
    	$paramArray["sign"] = $sign;
    
    	$paramsStr = http_build_query($paramArray); //请求参数str
    	$response = httpPost($payHost . "/api/pay/create_order", $paramsStr);
    	$resjson = json_decode($response, true);
    	
       
        header('Location:'.$resjson['payParams']['payUrl']);  
        exit;
        echo $resjson['payParams']['payUrl'];
        
            
    }
    
    public function bluePayfjhuitiao()
    {
        //商户秘钥
    	$mchKey = 'AQWBWJOQHDVOWHMM5B1EMEYMFAXPCQNPXSBEA3A01DCJLKJ2FPMXA3ETO68ZIRBQ2Z1USHTE0N222UA5CBOPUTBQHXP3DEDR9A8BRAXZ86KPDU19YO4RG7KB0GHFFI0J';
    	
        if(!isset($_REQUEST["sign"]) ){
    		echo "fail(sign not exists)";
    		exit;
    	}
    	
    	
    
    		
    	$resSign = $_REQUEST["sign"] ;
    	
    
    	
    	$paramArray = array();
    	
    	if(isset($_REQUEST["payOrderId"]) ){
    		$paramArray["payOrderId"] = $_REQUEST["payOrderId"];
    	}
    
    	if(isset($_REQUEST["income"]) ){
    		$paramArray["income"] = $_REQUEST["income"];
    	}
    	
    	if(isset($_REQUEST["mchId"]) ){
    		$paramArray["mchId"] = $_REQUEST["mchId"];
    	}
    	
    	if(isset($_REQUEST["appId"]) ){
    		$paramArray["appId"] = $_REQUEST["appId"];
    	}
    	
    	if(isset($_REQUEST["productId"]) ){
    		$paramArray["productId"] = $_REQUEST["productId"];
    	}
    	
    	if(isset($_REQUEST["mchOrderNo"]) ){
    		$paramArray["mchOrderNo"] = $_REQUEST["mchOrderNo"];
    	}
    	
    	if(isset($_REQUEST["amount"]) ){
    		$paramArray["amount"] = $_REQUEST["amount"];
    	}
    	
    	if(isset($_REQUEST["status"]) ){
    		$paramArray["status"] = $_REQUEST["status"];
    	}
    	
    	if(isset($_REQUEST["channelOrderNo"]) ){
    		$paramArray["channelOrderNo"] = $_REQUEST["channelOrderNo"];
    	}
    	
    	if(isset($_REQUEST["channelAttach"]) ){
    		$paramArray["channelAttach"] = $_REQUEST["channelAttach"];
    	}
    	
    	if(isset($_REQUEST["paySuccTime"]) ){
    		$paramArray["paySuccTime"] = $_REQUEST["paySuccTime"];
    	}
    	
    	if(isset($_REQUEST["backType"]) ){
    		$paramArray["backType"] = $_REQUEST["backType"];
    	}
    	
    	$sign = paramArraySign($paramArray, $mchKey);  //签名
    
    // 	$log_name = "wxsign.log"; //log文件路径
    //     writefile($log_name, "【接收到的notify通知】:\n" . $resSign."+". $sign. "\n");
    	
    	if($resSign != $sign){  //验签失败
    		echo "fail(verify fail)";
    		exit;
    	}else{
    	    $shijian = date('Y-m-d H:i:s' , time());
            $out_trade_no = $paramArray['mchOrderNo']; //
            $weixin       = $paramArray['payOrderId']; //微信订单号
            $cymoney = $paramArray['amount']/100;
            $orderNo = $out_trade_no; //我提交的订单号
            // $openid  = $notify->data["openid"];//微信openid
            
            $order = Cache::store('redis')->get($orderNo);
    		
    		
    		if($order['money'] != $cymoney){
    		   	  echo 'fail';
                  exit;
    		}
            $userid = $order['userid'];
            
            $zyid = $order['zyid'];
            $ip = $order['ip'];
            $ddh = $orderNo;
            $qudao = $ddh;
            $ipz = Db::name("ip")->where(["ddh2" => $ddh])->find();
            if (!empty($ipz))
            {
                exit;
            }
            
            if($order['order_type'] == 2){
                Db::name('baotian')->insert(['userid' => $order['userid'],'zyid' => $order['zyid'],'money' => $order['money'],'uopenid' => $order['uopenid'],'create_time' => time(),'end_time' => time() + 86400,'orderid' => $orderNo]);
            }
          
            $kl = Db::name("kou")->where(['userid' => $userid])->find();
            $ns = $kl['ns'];
            if (!empty($kl))
            {
                $cs = $kl['cs'] - 1;
                if ($cs <= 0)
                {
                    
                    $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                     
                    // $shipin = Db::name("siyou")->where(['id' => $zyid])->find();
                    $zymc = $shipin['name'];
                    $user = Db::name("member")->where(['id' => $userid])->find();
                    $name = $user['nickname'];
                    $data = [];
                    $data['name'] = $name;
                    $data['userid'] = $userid;
                    $data['ddh'] = $ddh;
                    $data['qudao'] = $qudao;
                    $data['money'] = $cymoney;
                    $data['shijian'] = $shijian;
                    $data['zymc'] = $zymc;
                    $data['type'] = $order['type'];
                     $data['order_type'] = $order['order_type'];
                    Db::name("kl")->insert($data);
                    Db::name("kou")->where(['userid' => $userid])->update(['cs' => $ns]);
                    // 不扣量
                    // Db::name("siyou")->where(['id'=>$zyid])->setInc("cs");
                    //  Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                    // //
                    $ddh2 = substr($ddh, 0, 10);
                    $data = [];
                    $data['ip'] = $ip;
                    $data['userid'] = $userid;
                    $data['ddh'] = $ddh2;
                    $data['zyid'] = $zyid;
                    $data['ddh2'] = $ddh;
                    $data['zt'] = "已付";
                    $data['shijian'] = time();
                    $data['openid']  = $order['uopenid'];
                    $data['type'] = $order['type'];
                   
                    $data['uopenid']  = $order['uopenid'];
                    $data['end_time']  = time() + 86400;
                    $data['order_type'] = $order['order_type'];
                    Db::name("ip")->insert($data);
                }
                else
                {
                    Db::name("kou")->where(['userid' => $userid])->setDec("cs"); 
                    // 写自己的数据库
                    $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                    if (empty($row))
                    { 
                        // /更新会员金额
                        $user = Db::name("member")->where(['id' => $userid])->find();
                        if (!empty($user))
                        {
                            if ($cymoney < 1)
                            {
                                $money2 = $cymoney;
                            }
                            else
                            {
                                $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                            }
                            $money = $user['money'] + $money2;
                            Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                        }
                        
                    
                        $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                        $zymc = $shipin['name'];
                        $data = [];
                        $data['zyid'] = $zyid;
                        $data['userid'] = $userid;
                        $data['zymc'] = $zymc;
                        $data['ddh'] = $ddh;
                        $data['money'] = $cymoney;
                        $data['shijian'] = $shijian;
                        $data['wxddh'] = $weixin;
                        $data['type'] = $order['type'];
                         $data['order_type'] = $order['order_type'];
                        Db::name("dingdan")->insert($data);
                        $ddh2 = substr($ddh, 0, 10);
                        $data = [];
                        $data['ip'] = $ip;
                        $data['userid'] = $userid;
                        $data['ddh'] = $ddh2;
                        $data['zyid'] = $zyid;
                        $data['ddh2'] = $ddh;
                        $data['zt'] = "已付";
                        $data['shijian'] = time();
                        $data['openid']  = $order['uopenid'];
                        $data['type'] = $order['type'];
                        $data['uopenid']  = $order['uopenid'];
                        $data['end_time']  = time() + 86400;
                        $data['order_type'] = $order['order_type'];
                        Db::name("ip")->insert($data);
                    } 
                    // 写自己的数据库
                } 
                // 不扣量
                // 代理ID没添加扣量参数
            }
            else
            { 
                // 写自己的数据库
                $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                if (empty($row))
                { 
                    // /更新会员金额
                    $user = Db::name("member")->where(['id' => $userid])->find();
                    if (!empty($user))
                    {
                        if ($cymoney < 1)
                        {
                            $money2 = $cymoney;
                        }
                        else
                        {
                            $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                        }
                        $money = $user['money'] + $money2;
                        Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                    }
                    $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                    $zymc = $shipin['name'];
                    $data = [];
                    $data['zyid'] = $zyid;
                    $data['userid'] = $userid;
                    $data['zymc'] = $zymc;
                    $data['ddh'] = $ddh;
                    $data['money'] = $cymoney;
                    $data['shijian'] = $shijian;
                    $data['wxddh'] = $weixin;
                    $data['type'] = $order['type'];
                    $data['order_type'] = $order['order_type'];
                    Db::name("dingdan")->insert($data);
                    $ddh2 = substr($ddh, 0, 10);
                    $data = [];
                    $data['ip'] = $ip;
                    $data['userid'] = $userid;
                    $data['ddh'] = $ddh2;
                    $data['zyid'] = $zyid;
                    $data['ddh2'] = $ddh;
                    $data['zt'] = "已付";
                    $data['shijian']  = time();
                    $data['openid']   = $order['uopenid'];
                    $data['uopenid']  = $order['uopenid'];
                    $data['type'] = $order['type'];
                    $data['end_time']   = time() + 86400;
                    $data['order_type'] = $order['order_type'];
                    Db::name("ip")->insert($data);
                }
            } 
    	}
    	//处理业务
    	
    	echo "success";
    	exit;
    }
    
    public function bluePay()
    {
        
        if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone')||strpos($_SERVER['HTTP_USER_AGENT'], 'iPad')){
              $ip = getIP();
          
        }else if(strpos($_SERVER['HTTP_USER_AGENT'], 'Android')){
              $ip = explode(',', getIP())[0];
        
        }else{
              $ip = getIP();
        }
       
        $ubodingdan = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES)); //提交订单号 . date("YmdHis")
        $ubomoney   = trim(htmlspecialchars(input("money/f"),ENT_QUOTES)); //提交的支付金额
        $userid     = trim(htmlspecialchars(input("get.userid"),ENT_QUOTES)); //提交的用户ID
        $ddh3       = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES));
        $zyid       = trim(htmlspecialchars(input("zyid/d"),ENT_QUOTES));
        $code       = trim(htmlspecialchars(input("get.code"),ENT_QUOTES));
        $uopenid    = trim(htmlspecialchars(input("get.uopenid",''),ENT_QUOTES));
        $pid        = trim(htmlspecialchars(input("get.pid",''),ENT_QUOTES));
        $is_baotian = trim(htmlspecialchars(input("get.is_baotian"),ENT_QUOTES));
        $b_money    = trim(htmlspecialchars(input("get.b_money"),ENT_QUOTES));
      
       
        $yifutime = time() - intval(24) * 3600;
        
        $ename = Cache::store('redis')->get('ylluodi');
      
        if(empty($ubodingdan) || empty($ubomoney) || empty($userid) || empty($ddh3) || empty($zyid) || empty($code) || empty($uopenid)){
           
            
             die('参数错误,请重新进入？');
            
        }
        
         session('useridopenid', $uopenid);
        
         $info = Db::name('baotian')->where(['uopenid' => $uopenid])->order('id desc')->find();
        
        if($info && $info['end_time'] > time()){
           
            $geturl = "http://".$ename."/index/index/shipinok.html?zyid=" . $info['zyid'] ."&ddh=".$info['orderid']."&uopenid=".$uopenid."&bzyid=".$zyid;
            
          
            $this->redirect($geturl);
        }
        
         $info = Cache::store('redis')->get($userid);
    
        if($info == 'null' || !$info){
            $info = Db::name('dwz')->where(['ddh' => $ddh3])->find();
        }
        
         $jiami_id = $userid;
        
         $userid = $info['userid'];
         
        
        $liebiao = Cache::store('redis')->has('ylgongyou'.$zyid);

		if(!$liebiao || $liebiao === "null"){
		      $liebiao = Db::name("gongyou")->where(['id' => $zyid])->find();
		     
		      Cache::store('redis')->set('ylgongyou'.$zyid,json_encode($liebiao),7879680);
		}
         
        $member = Db::name('member')->where(['id' => $userid])->find();
         
       
         
       
        if($b_money >= 30 && $is_baotian == 1){
            
             
              $order_type = 2;
              $money      = Db::name('pian')->where(['userid' => $userid])->value('b_money');
             
              if($money != $b_money){
                 die('参数错误');
              }
             
              $ubomoney = $money;
             
           
            //   $longurl = "http://".$ename ."/index/fang/index.html?userid=" . $jiami_id . '&zyid=' . $zyid . "&code=" . $jiami_id."&uopenid=".$uopenid. '&money=' . $ubomoney."&ddh=".$ddh3."&is_baotian=1"."&b_money=".$b_money;
            //   $this->redirect($longurl);
            
             
        }else{
             $order_type = 1;
             $ubomoney = Db::name('pian')->where(['userid' => $userid])->value('money');
            
            
        }
        
   
            
            
      
        
       
        
        
        
        
        $ubodingdannew = $ubodingdan . random(4) . $userid . date("dHis");
       
        //$ubodingdannew = $ubodingdan . random(4) . $userid . date("YmdHis");
        $order = Db::name("order")->where(['orderid' => $ubodingdannew])->field('orderid,id')->find();
        
      
       
        if (empty($order))
        {
            $data = [];
            $data['orderid'] = $ubodingdannew;
            $data['ip'] = $ip;
            $data['userid'] = $userid;
            $data['zyid'] = $zyid;
            $data['shijian'] = time();
            $data['money'] = $ubomoney;
            $data['uopenid'] = $uopenid;
            $data['code']    = $code . '|' . $ubodingdan;
            $data['ddh3']    = $ddh3; 
            $data['type']    = 3; //蓝帽支付
            $data['jiami']    = $jiami_id; 
            $data['order_type']    = $order_type;
            
            Cache::store('redis')->set($ubodingdannew,json_encode($data),86400);
            
            Db::name("order")->insert($data);
       
        }
        
        $notify_url = "http://".$ename."/index/pay/bluePayhuitiao";
        
        $return_url = "http://".$ename."/index/index/shipinok?ddh=" . $ubodingdannew;
        
        //商户ID
    	$mchId = '20000248';
    	//商户秘钥
    	$mchKey = 'SSE3RO6C0NPR4BKBVBB9CCHDXF3JADSIHRKYF4RQDOKWJV98GJAHQBMKZTEBYDN6KZR0UDDF2MY1UXBM3AHVXSJCJAFOITXA7JGTERVBAZ2ESNV5VRVNZPH9QHP6AJOV';
    	//商户应用ID
    	$appId = '1c0a00b816da47349d5893010846f2c0';
    	//支付产品ID
    	$productId= '8004';
    	//支付网关地址
    	$payHost = 'http://api.fdmbiotech.com';
        
        $amount =  $ubomoney * 100; //元转换为分
        $paramArray = array(
    		"mchId" => $mchId, //商户ID
    		"appId" => $appId,  //商户应用ID
    		"productId" => $productId,  //支付产品ID
    		"mchOrderNo" => $ubodingdannew,// 商户订单号
    		"currency" => 'cny',//币种
    		"amount" => $amount . "", //支付金额,单位分
    		//"clientIp" => '210.73.10.148',   //客户端IP
    		//"device" => 'ios10.3.1',    //客户端设备
    		"returnUrl" => $return_url,	 //支付结果前端跳转URL
    		"notifyUrl" => $notify_url,	 //支付结果后台回调URL
    		"subject" => '网络购物',	 //商品主题
    		"body" => '网络购物',	 //商品描述信息
    		//"param1" => '',	 //扩展参数1
    		//"param2" =>  '',	 //扩展参数2
    		"extra" =>  '附加参数'	 //附加参数
        );
       
    	$sign = paramArraySign($paramArray, $mchKey);  //签名
    	$paramArray["sign"] = $sign;
    
    	$paramsStr = http_build_query($paramArray); //请求参数str
    	$response = httpPost($payHost . "/api/pay/create_order", $paramsStr);
    	$resjson = json_decode($response, true);
    
        echo $resjson['payParams']['payUrl'];
        
            
    }
    
    public function bluePayhuitiao()
    {
        //商户秘钥
    	$mchKey = 'SSE3RO6C0NPR4BKBVBB9CCHDXF3JADSIHRKYF4RQDOKWJV98GJAHQBMKZTEBYDN6KZR0UDDF2MY1UXBM3AHVXSJCJAFOITXA7JGTERVBAZ2ESNV5VRVNZPH9QHP6AJOV';
    	
        if(!isset($_REQUEST["sign"]) ){
    		echo "fail(sign not exists)";
    		exit;
    	}
    	
    	
    
    		
    	$resSign = $_REQUEST["sign"] ;
    	
    
    	
    	$paramArray = array();
    	
    	if(isset($_REQUEST["payOrderId"]) ){
    		$paramArray["payOrderId"] = $_REQUEST["payOrderId"];
    	}
    
    	if(isset($_REQUEST["income"]) ){
    		$paramArray["income"] = $_REQUEST["income"];
    	}
    	
    	if(isset($_REQUEST["mchId"]) ){
    		$paramArray["mchId"] = $_REQUEST["mchId"];
    	}
    	
    	if(isset($_REQUEST["appId"]) ){
    		$paramArray["appId"] = $_REQUEST["appId"];
    	}
    	
    	if(isset($_REQUEST["productId"]) ){
    		$paramArray["productId"] = $_REQUEST["productId"];
    	}
    	
    	if(isset($_REQUEST["mchOrderNo"]) ){
    		$paramArray["mchOrderNo"] = $_REQUEST["mchOrderNo"];
    	}
    	
    	if(isset($_REQUEST["amount"]) ){
    		$paramArray["amount"] = $_REQUEST["amount"];
    	}
    	
    	if(isset($_REQUEST["status"]) ){
    		$paramArray["status"] = $_REQUEST["status"];
    	}
    	
    	if(isset($_REQUEST["channelOrderNo"]) ){
    		$paramArray["channelOrderNo"] = $_REQUEST["channelOrderNo"];
    	}
    	
    	if(isset($_REQUEST["channelAttach"]) ){
    		$paramArray["channelAttach"] = $_REQUEST["channelAttach"];
    	}
    	
    	if(isset($_REQUEST["paySuccTime"]) ){
    		$paramArray["paySuccTime"] = $_REQUEST["paySuccTime"];
    	}
    	
    	if(isset($_REQUEST["backType"]) ){
    		$paramArray["backType"] = $_REQUEST["backType"];
    	}
    	
    	$sign = paramArraySign($paramArray, $mchKey);  //签名
    
    // 	$log_name = "wxsign.log"; //log文件路径
    //     writefile($log_name, "【接收到的notify通知】:\n" . $resSign."+". $sign. "\n");
    	
    	if($resSign != $sign){  //验签失败
    		echo "fail(verify fail)";
    		exit;
    	}else{
    	    $shijian = date('Y-m-d H:i:s' , time());
            $out_trade_no = $paramArray['mchOrderNo']; //
            $weixin       = $paramArray['payOrderId']; //微信订单号
            $cymoney = $paramArray['amount']/100;
            $orderNo = $out_trade_no; //我提交的订单号
            // $openid  = $notify->data["openid"];//微信openid
            
            $order = Cache::store('redis')->get($orderNo);
    		
    		
    		if($order['money'] != $cymoney){
    		   	  echo 'fail';
                  exit;
    		}
            $userid = $order['userid'];
            
            $zyid = $order['zyid'];
            $ip = $order['ip'];
            $ddh = $orderNo;
            $qudao = $ddh;
            $ipz = Db::name("ip")->where(["ddh2" => $ddh])->find();
            if (!empty($ipz))
            {
                exit;
            }
            
            if($order['order_type'] == 2){
                Db::name('baotian')->insert(['userid' => $order['userid'],'zyid' => $order['zyid'],'money' => $order['money'],'uopenid' => $order['uopenid'],'create_time' => time(),'end_time' => time() + 86400,'orderid' => $orderNo]);
            }
          
            $kl = Db::name("kou")->where(['userid' => $userid])->find();
            $ns = $kl['ns'];
            if (!empty($kl))
            {
                $cs = $kl['cs'] - 1;
                if ($cs <= 0)
                {
                    
                    $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                     
                    // $shipin = Db::name("siyou")->where(['id' => $zyid])->find();
                    $zymc = $shipin['name'];
                    $user = Db::name("member")->where(['id' => $userid])->find();
                    $name = $user['nickname'];
                    $data = [];
                    $data['name'] = $name;
                    $data['userid'] = $userid;
                    $data['ddh'] = $ddh;
                    $data['qudao'] = $qudao;
                    $data['money'] = $cymoney;
                    $data['shijian'] = $shijian;
                    $data['zymc'] = $zymc;
                    $data['type'] = $order['type'];
                     $data['order_type'] = $order['order_type'];
                    Db::name("kl")->insert($data);
                    Db::name("kou")->where(['userid' => $userid])->update(['cs' => $ns]);
                    // 不扣量
                    // Db::name("siyou")->where(['id'=>$zyid])->setInc("cs");
                    //  Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                    // //
                    $ddh2 = substr($ddh, 0, 10);
                    $data = [];
                    $data['ip'] = $ip;
                    $data['userid'] = $userid;
                    $data['ddh'] = $ddh2;
                    $data['zyid'] = $zyid;
                    $data['ddh2'] = $ddh;
                    $data['zt'] = "已付";
                    $data['shijian'] = time();
                    $data['openid']  = $order['uopenid'];
                    $data['type'] = $order['type'];
                   
                    $data['uopenid']  = $order['uopenid'];
                    $data['end_time']  = time() + 86400;
                    $data['order_type'] = $order['order_type'];
                    Db::name("ip")->insert($data);
                }
                else
                {
                    Db::name("kou")->where(['userid' => $userid])->setDec("cs"); 
                    // 写自己的数据库
                    $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                    if (empty($row))
                    { 
                        // /更新会员金额
                        $user = Db::name("member")->where(['id' => $userid])->find();
                        if (!empty($user))
                        {
                            if ($cymoney < 1)
                            {
                                $money2 = $cymoney;
                            }
                            else
                            {
                                $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                            }
                            $money = $user['money'] + $money2;
                            Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                        }
                        
                    
                        $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                        $zymc = $shipin['name'];
                        $data = [];
                        $data['zyid'] = $zyid;
                        $data['userid'] = $userid;
                        $data['zymc'] = $zymc;
                        $data['ddh'] = $ddh;
                        $data['money'] = $cymoney;
                        $data['shijian'] = $shijian;
                        $data['wxddh'] = $weixin;
                        $data['type'] = $order['type'];
                         $data['order_type'] = $order['order_type'];
                        Db::name("dingdan")->insert($data);
                        $ddh2 = substr($ddh, 0, 10);
                        $data = [];
                        $data['ip'] = $ip;
                        $data['userid'] = $userid;
                        $data['ddh'] = $ddh2;
                        $data['zyid'] = $zyid;
                        $data['ddh2'] = $ddh;
                        $data['zt'] = "已付";
                        $data['shijian'] = time();
                        $data['openid']  = $order['uopenid'];
                        $data['type'] = $order['type'];
                        $data['uopenid']  = $order['uopenid'];
                        $data['end_time']  = time() + 86400;
                        $data['order_type'] = $order['order_type'];
                        Db::name("ip")->insert($data);
                    } 
                    // 写自己的数据库
                } 
                // 不扣量
                // 代理ID没添加扣量参数
            }
            else
            { 
                // 写自己的数据库
                $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                if (empty($row))
                { 
                    // /更新会员金额
                    $user = Db::name("member")->where(['id' => $userid])->find();
                    if (!empty($user))
                    {
                        if ($cymoney < 1)
                        {
                            $money2 = $cymoney;
                        }
                        else
                        {
                            $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                        }
                        $money = $user['money'] + $money2;
                        Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                    }
                    $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                    $zymc = $shipin['name'];
                    $data = [];
                    $data['zyid'] = $zyid;
                    $data['userid'] = $userid;
                    $data['zymc'] = $zymc;
                    $data['ddh'] = $ddh;
                    $data['money'] = $cymoney;
                    $data['shijian'] = $shijian;
                    $data['wxddh'] = $weixin;
                    $data['type'] = $order['type'];
                    $data['order_type'] = $order['order_type'];
                    Db::name("dingdan")->insert($data);
                    $ddh2 = substr($ddh, 0, 10);
                    $data = [];
                    $data['ip'] = $ip;
                    $data['userid'] = $userid;
                    $data['ddh'] = $ddh2;
                    $data['zyid'] = $zyid;
                    $data['ddh2'] = $ddh;
                    $data['zt'] = "已付";
                    $data['shijian']  = time();
                    $data['openid']   = $order['uopenid'];
                    $data['uopenid']  = $order['uopenid'];
                    $data['type'] = $order['type'];
                    $data['end_time']   = time() + 86400;
                    $data['order_type'] = $order['order_type'];
                    Db::name("ip")->insert($data);
                }
            } 
    	}
    	//处理业务
    	
    	echo "success";
    	exit;
    }
    
    
    
    public function bananapay()
    {
        
        
        
        if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone')||strpos($_SERVER['HTTP_USER_AGENT'], 'iPad')){
              $ip = getIP();
          
        }else if(strpos($_SERVER['HTTP_USER_AGENT'], 'Android')){
              $ip = explode(',', getIP())[0];
        
        }else{
              $ip = getIP();
        }
       
        $ubodingdan = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES)); //提交订单号 . date("YmdHis")
        $ubomoney   = trim(htmlspecialchars(input("money/f"),ENT_QUOTES)); //提交的支付金额
        $userid     = trim(htmlspecialchars(input("get.userid"),ENT_QUOTES)); //提交的用户ID
        $ddh3       = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES));
        $zyid       = trim(htmlspecialchars(input("zyid/d"),ENT_QUOTES));
        $code       = trim(htmlspecialchars(input("get.code"),ENT_QUOTES));
        $uopenid    = trim(htmlspecialchars(input("get.uopenid",''),ENT_QUOTES));
        $pid        = trim(htmlspecialchars(input("get.pid",''),ENT_QUOTES));
        $is_baotian = trim(htmlspecialchars(input("get.is_baotian"),ENT_QUOTES));
        $b_money    = trim(htmlspecialchars(input("get.b_money"),ENT_QUOTES));
      
       
        $yifutime = time() - intval(24) * 3600;
        
        $ename = Cache::store('redis')->get('ylluodi');
      
        if(empty($ubodingdan) || empty($ubomoney) || empty($userid) || empty($ddh3) || empty($zyid) || empty($code) || empty($uopenid)){
           
            
             die('参数错误,请重新进入？');
            
        }
        
         session('useridopenid', $uopenid);
        
        $info = Db::name('baotian')->where(['uopenid' => $uopenid])->order('id desc')->find();
        
        if($info && $info['end_time'] > time()){
           
            $geturl = "http://".$ename."/index/index/shipinok.html?zyid=" . $info['zyid'] ."&ddh=".$info['orderid']."&uopenid=".$uopenid."&bzyid=".$zyid;
            
            $this->redirect($geturl);
        }
        
         $info = Cache::store('redis')->get($userid);
    
        if($info == 'null' || !$info){
            $info = Db::name('dwz')->where(['ddh' => $ddh3])->find();
        }
        
         $jiami_id = $userid;
        
         $userid = $info['userid'];
         
        
        $liebiao = Cache::store('redis')->has('ylgongyou'.$zyid);

		if(!$liebiao || $liebiao === "null"){
		      $liebiao = Db::name("gongyou")->where(['id' => $zyid])->find();
		     
		      Cache::store('redis')->set('ylgongyou'.$zyid,json_encode($liebiao),7879680);
		}
         
          $member = Db::name('member')->where(['id' => $userid])->find();
         
        // if($member['zftd'] == 6){
        //   $longurl = "http://".$ename ."/index/fang/index.html?userid=" . $jiami_id . '&zyid=' . $zyid . "&code=" . $jiami_id."&uopenid=".$uopenid. '&money=' . $ubomoney."&ddh=".$ddh3;
        //          $this->redirect($longurl);
        // }
       
        if($b_money >= 30 && $is_baotian == 1){
            
             
            $order_type = 2;
            $money      = Db::name('pian')->where(['userid' => $userid])->value('b_money');
         
            if($money != $b_money){
                die('参数错误');
            }
         
            $ubomoney = $money;
          
            // $longurl = "http://".$ename ."/index/pay/saoma.html?userid=" . $jiami_id . '&zyid=' . $zyid . "&code=" . $jiami_id."&uopenid=".$uopenid. '&money=' . $ubomoney."&ddh=".$ddh3."&is_baotian=1"."&b_money=".$b_money;
            // $this->redirect($longurl);
        }else{
            $order_type = 1;
            $ubomoney   = Db::name('pian')->where(['userid' => $userid])->value('money');
           
        }
            
            
      
        
       
        
        
        
        
        $ubodingdannew = $ubodingdan . random(4) . $userid . date("dHis");
       
        //$ubodingdannew = $ubodingdan . random(4) . $userid . date("YmdHis");
        $order = Db::name("order")->where(['orderid' => $ubodingdannew])->field('orderid,id')->find();
        
      
       
        if (empty($order))
        {
            $data = [];
            $data['orderid'] = $ubodingdannew;
            $data['ip'] = $ip;
            $data['userid'] = $userid;
            $data['zyid'] = $zyid;
            $data['shijian'] = time();
            $data['money'] = $ubomoney;
            $data['uopenid'] = $uopenid;
            $data['code']    = $code . '|' . $ubodingdan;
            $data['ddh3']    = $ddh3; 
            $data['type']    = 6; //黄秋生支付
            $data['jiami']    = $jiami_id; 
            $data['order_type']    = $order_type; 
            
            Cache::store('redis')->set($ubodingdannew,json_encode($data),86400);
            
            Db::name("order")->insert($data);
       
        }
        
           
        
            $payurl = "http://bw.zmkh.top/order/payment/create";
            
            $notify_url = "http://".$ename."/index/pay/bananahuidiao";
            
            
            
    
            $return_url = "http://".$ename."/index/index/shipinok?ddh=" . $ubodingdannew;
            
            $merchantKey = "a1e8ff2666ed47b5ba77407ce6661aa8";
            
           
            $body['amount'] = $ubomoney * 100;
            $body['channelType'] = "2:1";
            $body['merchantOrderCode'] = $ubodingdannew;
            $body['noticeUrl'] = $notify_url;
            $body['returnUrl'] = $return_url;
            $signStr = '';
            foreach ($body as $key => $value){
              $signStr .= $key . '=' . $value;
            }
            $signStr .= 'key=' . $merchantKey;
            
            $paydata['merchantCode'] = "1615353129760";
            $paydata['body'] = $body;
            $sign = strtolower(md5($signStr));
            $paydata['sign'] = $sign;
            
            $ch = curl_init($payurl);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($paydata));
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
            $response = curl_exec($ch);
            curl_close($ch);
            $resdata = json_decode($response,true);
            if($resdata['code'] == 200){
             
                $this->redirect($resdata['body']['paymentUrl']);
            }
            
    }
    
    
    public function bananapaygema()
    {
        
        if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone')||strpos($_SERVER['HTTP_USER_AGENT'], 'iPad')){
              $ip = getIP();
          
        }else if(strpos($_SERVER['HTTP_USER_AGENT'], 'Android')){
              $ip = explode(',', getIP())[0];
        
        }else{
              $ip = getIP();
        }
       
        $ubodingdan = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES)); //提交订单号 . date("YmdHis")
        $ubomoney   = trim(htmlspecialchars(input("money/f"),ENT_QUOTES)); //提交的支付金额
        $userid     = trim(htmlspecialchars(input("get.userid"),ENT_QUOTES)); //提交的用户ID
        $ddh3       = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES));
        $zyid       = trim(htmlspecialchars(input("zyid/d"),ENT_QUOTES));
        $code       = trim(htmlspecialchars(input("get.code"),ENT_QUOTES));
        $uopenid    = trim(htmlspecialchars(input("get.uopenid",''),ENT_QUOTES));
        $pid        = trim(htmlspecialchars(input("get.pid",''),ENT_QUOTES));
        $is_baotian = trim(htmlspecialchars(input("get.is_baotian"),ENT_QUOTES));
        $b_money    = trim(htmlspecialchars(input("get.b_money"),ENT_QUOTES));
      
       
        $yifutime = time() - intval(24) * 3600;
        
        $ename = Cache::store('redis')->get('ylluodi');
      
        if(empty($ubodingdan) || empty($ubomoney) || empty($userid) || empty($ddh3) || empty($zyid) || empty($code) || empty($uopenid)){
           
            
             die('参数错误,请重新进入？');
            
        }
        
         session('useridopenid', $uopenid);
        
        $info = Db::name('baotian')->where(['uopenid' => $uopenid])->order('id desc')->find();
        
        if($info && $info['end_time'] > time()){
           
            $geturl = "http://".$ename."/index/index/shipinok.html?zyid=" . $info['zyid'] ."&ddh=".$info['orderid']."&uopenid=".$uopenid."&bzyid=".$zyid;
            
            $this->redirect($geturl);
        }
        
         $info = Cache::store('redis')->get($userid);
    
        if($info == 'null' || !$info){
            $info = Db::name('dwz')->where(['ddh' => $ddh3])->find();
        }
        
         $jiami_id = $userid;
        
         $userid = $info['userid'];
         
        
        $liebiao = Cache::store('redis')->has('ylgongyou'.$zyid);

		if(!$liebiao || $liebiao === "null"){
		      $liebiao = Db::name("gongyou")->where(['id' => $zyid])->find();
		     
		      Cache::store('redis')->set('ylgongyou'.$zyid,json_encode($liebiao),7879680);
		}
         
          $member = Db::name('member')->where(['id' => $userid])->find();
         
        // if($member['zftd'] == 6){
        //   $longurl = "http://".$ename ."/index/fang/index.html?userid=" . $jiami_id . '&zyid=' . $zyid . "&code=" . $jiami_id."&uopenid=".$uopenid. '&money=' . $ubomoney."&ddh=".$ddh3;
        //          $this->redirect($longurl);
        // }
        
        
        
       
        if($b_money >= 30 && $is_baotian == 1){
            
             
            $order_type = 2;
            $money      = Db::name('pian')->where(['userid' => $userid])->value('b_money');
         
            if($money != $b_money){
                die('参数错误');
            }
         
            $ubomoney = $money;
          
            $longurl = "http://".$ename ."/index/pay/saoma.html?userid=" . $jiami_id . '&zyid=' . $zyid . "&code=" . $jiami_id."&uopenid=".$uopenid. '&money=' . $ubomoney."&ddh=".$ddh3."&is_baotian=1"."&b_money=".$b_money;
            $this->redirect($longurl);
        }else{
            $order_type = 1;
            $ubomoney   = Db::name('pian')->where(['userid' => $userid])->value('money');
            
             if($ubomoney >= 10){
                $longurl = "http://".$ename ."/index/pay/saoma.html?userid=" . $jiami_id . '&zyid=' . $zyid . "&code=" . $jiami_id."&uopenid=".$uopenid. '&money=' . $ubomoney."&ddh=".$ddh3;
                $this->redirect($longurl);
            }
           
        }
        
        
        
        $gema         = Cache::store('redis')->get("ylgema");//个码开关
        
        if($gema == 0){
            
             if($b_money >= 30 && $is_baotian == 1){
                    $longurl = "http://".$ename ."/index/pay/saoma.html?userid=" . $jiami_id . '&zyid=' . $zyid . "&code=" . $jiami_id."&uopenid=".$uopenid. '&money=' . $ubomoney."&ddh=".$ddh3."&is_baotian=1"."&b_money=".$b_money;
                }else{
                    $longurl = "http://".$ename ."/index/pay/saoma.html?userid=" . $jiami_id . '&zyid=' . $zyid . "&code=" . $jiami_id."&uopenid=".$uopenid. '&money=' . $ubomoney."&ddh=".$ddh3;
                }
            
        }
        
        
        $ubomoney = ceil($ubomoney);
            
            
      
        
       
        
        
        
        
        $ubodingdannew = $ubodingdan . random(4) . $userid . date("dHis");
       
        //$ubodingdannew = $ubodingdan . random(4) . $userid . date("YmdHis");
        $order = Db::name("order")->where(['orderid' => $ubodingdannew])->field('orderid,id')->find();
        
      
       
        if (empty($order))
        {
            $data = [];
            $data['orderid'] = $ubodingdannew;
            $data['ip'] = $ip;
            $data['userid'] = $userid;
            $data['zyid'] = $zyid;
            $data['shijian'] = time();
            $data['money'] = $ubomoney;
            $data['uopenid'] = $uopenid;
            $data['code']    = $code . '|' . $ubodingdan;
            $data['ddh3']    = $ddh3; 
            $data['type']    = 118; //黄秋生支付
            $data['jiami']    = $jiami_id; 
            $data['order_type']    = $order_type; 
            
            Cache::store('redis')->set($ubodingdannew,json_encode($data),86400);
            
            Db::name("order")->insert($data);
       
        }
        
           
        
            $payurl = "http://bw.etaoba.shop/order/payment/create";
            
            $notify_url = "http://".$ename."/index/pay/bananahuidiaogema";
            
            
            
    
            $return_url = "http://".$ename."/index/index/shipinok?ddh=" . $ubodingdannew;
            
            $merchantKey = "a1e8ff2666ed47b5ba77407ce6661aa8";
            
           
            $body['amount'] = $ubomoney * 100;
            $body['channelType'] = "2:2";
            $body['merchantOrderCode'] = $ubodingdannew;
            $body['noticeUrl'] = $notify_url;
            $body['returnUrl'] = $return_url;
            $signStr = '';
            foreach ($body as $key => $value){
              $signStr .= $key . '=' . $value;
            }
            $signStr .= 'key=' . $merchantKey;
            
            $paydata['merchantCode'] = "1615353129760";
            $paydata['body'] = $body;
            $sign = strtolower(md5($signStr));
            $paydata['sign'] = $sign;
            
            $ch = curl_init($payurl);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($paydata));
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
            $response = curl_exec($ch);
            curl_close($ch);
            $resdata = json_decode($response,true);
            if($resdata['code'] == 200){
             
                $this->redirect($resdata['body']['paymentUrl']);
            }
            
    }
    
    public function bananahuidiao()
    {
        
        $merchantKey = "a1e8ff2666ed47b5ba77407ce6661aa8";
        
        $post = file_get_contents('php://input');
        $log_name = "huang.log"; //log文件路径
        writefile($log_name, "【接收到的notify通知】:\n" . $post . "\n");
        $post = json_decode($post,true);
        
        $huangdata['amount']            = $post['amount'];
        $huangdata['merchantOrderCode'] = $post['merchantOrderCode'];//商户订单号-020+0
        $huangdata['platformOrderCode'] = $post['platformOrderCode'];
        $huangdata['status']            = $post['status'];
       
       
       
        
       
        $signStr = '';
        foreach ($huangdata as $key => $value){ 
          $signStr .= $key . '=' . $value;
        }
        $signStr .= 'key=' . $merchantKey;
        
       
        $sign = strtolower(md5($signStr));
        
        
      
        
        if ($sign !== $post['sign']) { 
           // 失败， 失败逻辑
            echo 'fail';
            exit;
        } else {
            writefile("huang1.log", "【接收到的notify通知】:\n" . $sign . "\n");
            
        	    $shijian = date('Y-m-d H:i:s' , time());
                $out_trade_no = $huangdata['merchantOrderCode']; //
                $weixin       = $huangdata['platformOrderCode']; //微信订单号
                $cymoney = $huangdata['amount']/100;
                $orderNo = $out_trade_no; //我提交的订单号
                // $openid  = $notify->data["openid"];//微信openid
                
                $order = Cache::store('redis')->get($orderNo);
        		
        		
        		if($order['money'] != $cymoney && $cymoney < 12){
        		   	  echo 'fail';
                      exit;
        		}
        		
        // 		$cymoney = $cymoney * 0.97;
                
                // $order = Db::name("order")->where(['orderid' => $orderNo])->field("userid,zyid,ip,uopenid,orderid")->find();
                
                $userid = $order['userid'];
                
                
                $zyid = $order['zyid'];
                $ip = $order['ip'];
                $ddh = $orderNo;
                $qudao = $ddh;
                $ipz = Db::name("ip")->where(["ddh2" => $ddh])->find();
                if (!empty($ipz))
                {
                    exit;
                }
                
                 if($order['order_type'] == 2){
                    Db::name('baotian')->insert(['userid' => $order['userid'],'zyid' => $order['zyid'],'money' => $order['money'],'uopenid' => $order['uopenid'],'create_time' => time(),'end_time' => time() + 86400,'orderid' => $orderNo]);
                }
                    $kl = Db::name("kou")->where(['userid' => $userid])->find();
                    $ns = $kl['ns'];
                    if (!empty($kl))
                    {
                        $cs = $kl['cs'] - 1;
                        if ($cs <= 0)
                        {
                            
                            $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                             
                            // $shipin = Db::name("siyou")->where(['id' => $zyid])->find();
                            $zymc = $shipin['name'];
                            $user = Db::name("member")->where(['id' => $userid])->find();
                            $name = $user['nickname'];
                            $data = [];
                            $data['name'] = $name;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh;
                            $data['qudao'] = $qudao;
                            $data['money'] = $cymoney;
                            $data['shijian'] = $shijian;
                            $data['zymc'] = $zymc;
                            $data['type'] = $order['type'];
                             $data['order_type'] = $order['order_type'];
                            Db::name("kl")->insert($data);
                            Db::name("kou")->where(['userid' => $userid])->update(['cs' => $ns]);
                            // 不扣量
                            // Db::name("siyou")->where(['id'=>$zyid])->setInc("cs");
                            // Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                            // //
                            $ddh2 = substr($ddh, 0, 10);
                            $data = [];
                            $data['ip'] = $ip;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh2;
                            $data['zyid'] = $zyid;
                            $data['ddh2'] = $ddh;
                            $data['zt'] = "已付";
                            $data['shijian'] = time();
                            $data['openid']  = $order['uopenid'];
                            $data['type'] = $order['type'];
                            $data['order_type'] = $order['order_type'];
                            $data['uopenid']  = $order['uopenid'];
                           
                            $data['end_time']  = time() + 86400;
                            Db::name("ip")->insert($data);
                        }
                        else
                        {
                            Db::name("kou")->where(['userid' => $userid])->setDec("cs"); 
                            // 写自己的数据库
                            $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                            if (empty($row))
                            { 
                                // /更新会员金额
                                $user = Db::name("member")->where(['id' => $userid])->find();
                                if (!empty($user))
                                {
                                    if ($cymoney < 1)
                                    {
                                        $money2 = $cymoney;
                                    }
                                    else
                                    {
                                        $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                                    }
                                    $money = $user['money'] + $money2;
                                    Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                                }
                                
                            
                                $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                                $zymc = $shipin['name'];
                                // if (!empty($shipin))
                                // {
                                //     // Db::name("siyou")->where(['id' => $zyid])->setInc("cs");
                                //  Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                                // }
                                $data = [];
                                $data['zyid'] = $zyid;
                                $data['userid'] = $userid;
                                $data['zymc'] = $zymc;
                                $data['ddh'] = $ddh;
                                $data['money'] = $cymoney;
                                $data['shijian'] = $shijian;
                                $data['wxddh'] = $weixin;
                                $data['type'] = $order['type'];
                                 $data['order_type'] = $order['order_type'];
                                Db::name("dingdan")->insert($data);
                                $ddh2 = substr($ddh, 0, 10);
                                $data = [];
                                $data['ip'] = $ip;
                                $data['userid'] = $userid;
                                $data['ddh'] = $ddh2;
                                $data['zyid'] = $zyid;
                                $data['ddh2'] = $ddh;
                                $data['zt'] = "已付";
                                $data['shijian'] = time();
                                $data['openid']  = $order['uopenid'];
                                $data['type'] = $order['type'];
                                $data['uopenid']  = $order['uopenid'];
                                $data['order_type'] = $order['order_type'];
                                 $data['end_time']  = time() + 86400;
                                Db::name("ip")->insert($data);
                            } 
                            // 写自己的数据库
                        } 
                        // 不扣量
                        // 代理ID没添加扣量参数
                    }
                    else
                    { 
                        // 写自己的数据库
                        $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                        if (empty($row))
                        { 
                            // /更新会员金额
                            $user = Db::name("member")->where(['id' => $userid])->find();
                            if (!empty($user))
                            {
                                if ($cymoney < 1)
                                {
                                    $money2 = $cymoney;
                                }
                                else
                                {
                                    $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                                }
                                $money = $user['money'] + $money2;
                                Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                            }
                            $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                            // $shipin = Db::name("siyou")->where(['id' => $zyid])->find();
                            $zymc = $shipin['name'];
                            // if (!empty($shipin))
                            // {
                            //     // Db::name("siyou")->where(['id' => $zyid])->setInc("cs");
                            // Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                            // }
                            $data = [];
                            $data['zyid'] = $zyid;
                            $data['userid'] = $userid;
                            $data['zymc'] = $zymc;
                            $data['ddh'] = $ddh;
                            $data['money'] = $cymoney;
                            $data['shijian'] = $shijian;
                            $data['wxddh'] = $weixin;
                            $data['type'] = $order['type'];
                             $data['order_type'] = $order['order_type'];
                            Db::name("dingdan")->insert($data);
                            $ddh2 = substr($ddh, 0, 10);
                            $data = [];
                            $data['ip'] = $ip;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh2;
                            $data['zyid'] = $zyid;
                            $data['ddh2'] = $ddh;
                            $data['zt'] = "已付";
                            $data['shijian']  = time();
                            $data['openid']   = $order['uopenid'];
                            $data['uopenid']  = $order['uopenid'];
                            $data['type'] = $order['type'];
                             $data['order_type'] = $order['order_type'];
                              $data['end_time']  = time() + 86400;
                            Db::name("ip")->insert($data);
                        } 
                        // 写自己的数据库
                    } 
          
            // 成功， 成功逻辑
            echo 'SUCCESS';
        }
     }
      public function bananahuidiaogema()
    {
        
        $merchantKey = "a1e8ff2666ed47b5ba77407ce6661aa8";
        
        $post = file_get_contents('php://input');
        $log_name = "huang.log"; //log文件路径
        writefile($log_name, "【接收到的notify通知】:\n" . $post . "\n");
        $post = json_decode($post,true);
        
        $huangdata['amount']            = $post['amount'];
        $huangdata['merchantOrderCode'] = $post['merchantOrderCode'];//商户订单号-020+0
        $huangdata['platformOrderCode'] = $post['platformOrderCode'];
        $huangdata['status']            = $post['status'];
       
       
       
        
       
        $signStr = '';
        foreach ($huangdata as $key => $value){ 
          $signStr .= $key . '=' . $value;
        }
        $signStr .= 'key=' . $merchantKey;
        
       
        $sign = strtolower(md5($signStr));
        
        
      
        
        if ($sign !== $post['sign']) { 
           // 失败， 失败逻辑
            echo 'fail';
            exit;
        } else {
            writefile("huang1.log", "【接收到的notify通知】:\n" . $sign . "\n");
            
        	    $shijian = date('Y-m-d H:i:s' , time());
                $out_trade_no = $huangdata['merchantOrderCode']; //
                $weixin       = $huangdata['platformOrderCode']; //微信订单号
                $cymoney = $huangdata['amount']/100;
                $orderNo = $out_trade_no; //我提交的订单号
                // $openid  = $notify->data["openid"];//微信openid
                
                $order = Cache::store('redis')->get($orderNo);
        		
        		
        		if($order['money'] != $cymoney && $cymoney < 12){
        		   	  echo 'fail';
                      exit;
        		}
        		
        // 		$cymoney = $cymoney * 0.97;
                
                // $order = Db::name("order")->where(['orderid' => $orderNo])->field("userid,zyid,ip,uopenid,orderid")->find();
                
                $userid = $order['userid'];
                
                
                $zyid = $order['zyid'];
                $ip = $order['ip'];
                $ddh = $orderNo;
                $qudao = $ddh;
                $ipz = Db::name("ip")->where(["ddh2" => $ddh])->find();
                if (!empty($ipz))
                {
                    exit;
                }
                
                 if($order['order_type'] == 2){
                    Db::name('baotian')->insert(['userid' => $order['userid'],'zyid' => $order['zyid'],'money' => $order['money'],'uopenid' => $order['uopenid'],'create_time' => time(),'end_time' => time() + 86400,'orderid' => $orderNo]);
                }
                    $kl = Db::name("kou")->where(['userid' => $userid])->find();
                    $ns = $kl['ns'];
                    if (!empty($kl))
                    {
                        $cs = $kl['cs'] - 1;
                        if ($cs <= 0)
                        {
                            
                            $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                             
                            // $shipin = Db::name("siyou")->where(['id' => $zyid])->find();
                            $zymc = $shipin['name'];
                            $user = Db::name("member")->where(['id' => $userid])->find();
                            $name = $user['nickname'];
                            $data = [];
                            $data['name'] = $name;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh;
                            $data['qudao'] = $qudao;
                            $data['money'] = $cymoney;
                            $data['shijian'] = $shijian;
                            $data['zymc'] = $zymc;
                            $data['type'] = $order['type'];
                             $data['order_type'] = $order['order_type'];
                            Db::name("kl")->insert($data);
                            Db::name("kou")->where(['userid' => $userid])->update(['cs' => $ns]);
                            // 不扣量
                            // Db::name("siyou")->where(['id'=>$zyid])->setInc("cs");
                            // Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                            // //
                            $ddh2 = substr($ddh, 0, 10);
                            $data = [];
                            $data['ip'] = $ip;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh2;
                            $data['zyid'] = $zyid;
                            $data['ddh2'] = $ddh;
                            $data['zt'] = "已付";
                            $data['shijian'] = time();
                            $data['openid']  = $order['uopenid'];
                            $data['type'] = $order['type'];
                            $data['order_type'] = $order['order_type'];
                            $data['uopenid']  = $order['uopenid'];
                           
                            $data['end_time']  = time() + 86400;
                            Db::name("ip")->insert($data);
                        }
                        else
                        {
                            Db::name("kou")->where(['userid' => $userid])->setDec("cs"); 
                            // 写自己的数据库
                            $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                            if (empty($row))
                            { 
                                // /更新会员金额
                                $user = Db::name("member")->where(['id' => $userid])->find();
                                if (!empty($user))
                                {
                                    if ($cymoney < 1)
                                    {
                                        $money2 = $cymoney;
                                    }
                                    else
                                    {
                                        $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                                    }
                                    $money = $user['money'] + $money2;
                                    Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                                }
                                
                            
                                $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                                $zymc = $shipin['name'];
                                // if (!empty($shipin))
                                // {
                                //     // Db::name("siyou")->where(['id' => $zyid])->setInc("cs");
                                //  Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                                // }
                                $data = [];
                                $data['zyid'] = $zyid;
                                $data['userid'] = $userid;
                                $data['zymc'] = $zymc;
                                $data['ddh'] = $ddh;
                                $data['money'] = $cymoney;
                                $data['shijian'] = $shijian;
                                $data['wxddh'] = $weixin;
                                $data['type'] = $order['type'];
                                 $data['order_type'] = $order['order_type'];
                                Db::name("dingdan")->insert($data);
                                $ddh2 = substr($ddh, 0, 10);
                                $data = [];
                                $data['ip'] = $ip;
                                $data['userid'] = $userid;
                                $data['ddh'] = $ddh2;
                                $data['zyid'] = $zyid;
                                $data['ddh2'] = $ddh;
                                $data['zt'] = "已付";
                                $data['shijian'] = time();
                                $data['openid']  = $order['uopenid'];
                                $data['type'] = $order['type'];
                                $data['uopenid']  = $order['uopenid'];
                                $data['order_type'] = $order['order_type'];
                                 $data['end_time']  = time() + 86400;
                                Db::name("ip")->insert($data);
                            } 
                            // 写自己的数据库
                        } 
                        // 不扣量
                        // 代理ID没添加扣量参数
                    }
                    else
                    { 
                        // 写自己的数据库
                        $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                        if (empty($row))
                        { 
                            // /更新会员金额
                            $user = Db::name("member")->where(['id' => $userid])->find();
                            if (!empty($user))
                            {
                                if ($cymoney < 1)
                                {
                                    $money2 = $cymoney;
                                }
                                else
                                {
                                    $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                                }
                                $money = $user['money'] + $money2;
                                Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                            }
                            $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                            // $shipin = Db::name("siyou")->where(['id' => $zyid])->find();
                            $zymc = $shipin['name'];
                            // if (!empty($shipin))
                            // {
                            //     // Db::name("siyou")->where(['id' => $zyid])->setInc("cs");
                            // Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                            // }
                            $data = [];
                            $data['zyid'] = $zyid;
                            $data['userid'] = $userid;
                            $data['zymc'] = $zymc;
                            $data['ddh'] = $ddh;
                            $data['money'] = $cymoney;
                            $data['shijian'] = $shijian;
                            $data['wxddh'] = $weixin;
                            $data['type'] = $order['type'];
                             $data['order_type'] = $order['order_type'];
                            Db::name("dingdan")->insert($data);
                            $ddh2 = substr($ddh, 0, 10);
                            $data = [];
                            $data['ip'] = $ip;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh2;
                            $data['zyid'] = $zyid;
                            $data['ddh2'] = $ddh;
                            $data['zt'] = "已付";
                            $data['shijian']  = time();
                            $data['openid']   = $order['uopenid'];
                            $data['uopenid']  = $order['uopenid'];
                            $data['type'] = $order['type'];
                             $data['order_type'] = $order['order_type'];
                              $data['end_time']  = time() + 86400;
                            Db::name("ip")->insert($data);
                        } 
                        // 写自己的数据库
                    } 
          
            // 成功， 成功逻辑
            echo 'SUCCESS';
        }
     }
     
    public function bananapaynew()
    {
        if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone')||strpos($_SERVER['HTTP_USER_AGENT'], 'iPad')){
              $ip = getIP();
          
        }else if(strpos($_SERVER['HTTP_USER_AGENT'], 'Android')){
              $ip = explode(',', getIP())[0];
        
        }else{
              $ip = getIP();
        }
       
        $ubodingdan = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES)); //提交订单号 . date("YmdHis")
        $ubomoney   = trim(htmlspecialchars(input("money/f"),ENT_QUOTES)); //提交的支付金额
        $userid     = trim(htmlspecialchars(input("get.userid"),ENT_QUOTES)); //提交的用户ID
        $ddh3       = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES));
        $zyid       = trim(htmlspecialchars(input("zyid/d"),ENT_QUOTES));
        $code       = trim(htmlspecialchars(input("get.code"),ENT_QUOTES));
        $uopenid    = trim(htmlspecialchars(input("get.uopenid",''),ENT_QUOTES));
        $pid        = trim(htmlspecialchars(input("get.pid",''),ENT_QUOTES));
        $is_baotian = trim(htmlspecialchars(input("get.is_baotian"),ENT_QUOTES));
        $b_money    = trim(htmlspecialchars(input("get.b_money"),ENT_QUOTES));
       
        $yifutime = time() - intval(24) * 3600;
        
        $ename = Cache::store('redis')->get('ylluodi');
      
        if(empty($ubodingdan) || empty($ubomoney) || empty($userid) || empty($ddh3) || empty($zyid) || empty($code) || empty($uopenid)){
           
            
             die('参数错误,请重新进入？');
            
        }
        
         //查询是否购买
         session('useridopenid', $uopenid);
        
         $info = Db::name('baotian')->where(['uopenid' => $uopenid])->order('id desc')->find();
        
        if($info && $info['end_time'] > time()){
           
            $geturl = "http://".$ename."/index/index/shipinok.html?zyid=" . $info['zyid'] ."&ddh=".$info['orderid']."&uopenid=".$uopenid."&bzyid=".$zyid;
            
          
            $this->redirect($geturl);
        }
        
         $info = Cache::store('redis')->get($userid);
    
        if($info == 'null' || !$info){
            $info = Db::name('dwz')->where(['ddh' => $ddh3])->find();
        }
        
         $jiami_id = $userid;
        
         $userid = $info['userid'];
         
        
         
        $liebiao = Cache::store('redis')->has('ylgongyou'.$zyid);

		if(!$liebiao || $liebiao === "null"){
		      $liebiao = Db::name("gongyou")->where(['id' => $zyid])->find();
		     
		      Cache::store('redis')->set('ylgongyou'.$zyid,json_encode($liebiao),7879680);
		}
         
        $member = Db::name('member')->where(['id' => $userid])->find();
         
        if($b_money >= 30 && $is_baotian == 1){
            
             
              $order_type = 2;
              $money      = Db::name('pian')->where(['userid' => $userid])->value('b_money');
             
              if($money != $b_money){
                 die('参数错误');
              }
             
              $ubomoney = $money; 
             
           
            
             
        }else{
             $order_type = 1;
             $ubomoney = Db::name('pian')->where(['userid' => $userid])->value('money');
            
            
        }
            
            
      
        
       
        
        
        
        
        $ubodingdannew = $ubodingdan . random(4) . $userid . date("dHis");
       
        //$ubodingdannew = $ubodingdan . random(4) . $userid . date("YmdHis");
        $order = Db::name("order")->where(['orderid' => $ubodingdannew])->field('orderid,id')->find();
        
      
       
        if (empty($order))
        {
            $data = [];
            $data['orderid'] = $ubodingdannew;
            $data['ip'] = $ip;
            $data['userid'] = $userid;
            $data['zyid'] = $zyid;
            $data['shijian'] = time();
            $data['money'] = $ubomoney;
            $data['uopenid'] = $uopenid;
            $data['code']    = $code . '|' . $ubodingdan;
            $data['ddh3']    = $ddh3; 
            $data['type']    = 9; //黄秋生支付
            $data['jiami']    = $jiami_id; 
            $data['order_type']  = $order_type; 
            
            Cache::store('redis')->set($ubodingdannew,json_encode($data),86400);
            
            Db::name("order")->insert($data);
       
        }
        
           
        
            $payurl = "http://8.133.181.215:8000/order/payment/create";
            
            $notify_url = "http://".$ename."/index/pay/bananahuidiaonew";
            
            
    
    
            $return_url = "http://".$ename."/index/index/shipinok?ddh=" . $ubodingdannew;
            
            $merchantKey = "bb6baf95292b486dbb11e96d162214be";
            
           
            $body['amount'] = $ubomoney * 100;
            $body['channelType'] = "2:1";
            $body['merchantOrderCode'] = $ubodingdannew;
            $body['noticeUrl'] = $notify_url;
            $body['returnUrl'] = $return_url;
            $signStr = '';
            foreach ($body as $key => $value){
              $signStr .= $key . '=' . $value;
            }
            $signStr .= 'key=' . $merchantKey;
            
            // var_dump($signStr);
            
            $paydata['merchantCode'] = "1616305116226";
            $paydata['body'] = $body;
            // $signdata = 'amount='.$body['amount'].'channelType='.$body['channelType'].'merchantOrderCode='.$paydata['merchantCode'].'noticeUrl='.$body['noticeUrl'].'returnUrl='. $return_url.'key='.$key;
            $sign = strtolower(md5($signStr));
            $paydata['sign'] = $sign;
            
            $ch = curl_init($payurl);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($paydata));
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
            $response = curl_exec($ch);
            curl_close($ch);
            $resdata = json_decode($response,true);
            if($resdata['code'] == 200){
             
                 $this->redirect($resdata['body']['paymentUrl']);
            }
            
    }
    
    public function bananahuidiaonew()
    {
        
       
        
        $merchantKey = "bb6baf95292b486dbb11e96d162214be";
        
        
        
        
        $post = file_get_contents('php://input');
        $log_name = "huang.log"; //log文件路径
        writefile($log_name, "【接收到的notify通知】:\n" . $post . "\n");
        $post = json_decode($post,true);
        
       
       
        $huangdata['amount']            = $post['amount'];
        $huangdata['merchantOrderCode'] = $post['merchantOrderCode'];//商户订单号-020+0
        $huangdata['platformOrderCode'] = $post['platformOrderCode'];
        $huangdata['status']            = $post['status'];
       
       
       
        
       
        $signStr = '';
        foreach ($huangdata as $key => $value){
          $signStr .= $key . '=' . $value;
        }
        $signStr .= 'key=' . $merchantKey;
        
       
        $sign = strtolower(md5($signStr));
        
        
      
        
        if ($sign !== $post['sign']) {
            
           // 失败， 失败逻辑
            echo 'fail';
            exit;
        } else {
            
        	    $shijian = date('Y-m-d H:i:s' , time());
                $out_trade_no = $huangdata['merchantOrderCode']; //
                $weixin       = $huangdata['platformOrderCode']; //微信订单号
                $cymoney = $huangdata['amount']/100;
                $orderNo = $out_trade_no; //我提交的订单号
                // $openid  = $notify->data["openid"];//微信openid
                
                $order = Cache::store('redis')->get($orderNo);
        		
        		
        		if($order['money'] != $cymoney && $cymoney < 12){
        		   	  echo 'fail';
                      exit;
        		}
        		
        // 		$cymoney = $cymoney * 0.97;
                
                // $order = Db::name("order")->where(['orderid' => $orderNo])->field("userid,zyid,ip,uopenid,orderid")->find();
                
                $userid = $order['userid'];
                
                
                $zyid = $order['zyid'];
                $ip = $order['ip'];
                $ddh = $orderNo;
                $qudao = $ddh;
                $ipz = Db::name("ip")->where(["ddh2" => $ddh])->find();
                if (!empty($ipz))
                {
                    exit;
                }
                
                if($order['order_type'] == 2){
                    Db::name('baotian')->insert(['userid' => $order['userid'],'zyid' => $order['zyid'],'money' => $order['money'],'uopenid' => $order['uopenid'],'create_time' => time(),'end_time' => time() + 86400,'orderid' => $orderNo]);
                }
                
                if(Cache::store('redis')->has($order['uopenid'])){

                    $zyidstr = Cache::store('redis')->get($order['uopenid']);
                  
                    $zyidstr = $zyidstr.','.$zyid;
                    
                    Cache::store('redis')->set($order['uopenid'],$zyidstr,604800);
                }else{
                    
                    Cache::store('redis')->set($order['uopenid'],$zyid,604800);
                   
                }
              
                    $kl = Db::name("kou")->where(['userid' => $userid])->find();
                    $ns = $kl['ns'];
                    if (!empty($kl))
                    {
                        $cs = $kl['cs'] - 1;
                        if ($cs <= 0)
                        {
                            
                            $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                             
                            // $shipin = Db::name("siyou")->where(['id' => $zyid])->find();
                            $zymc = $shipin['name'];
                            $user = Db::name("member")->where(['id' => $userid])->find();
                            $name = $user['nickname'];
                            $data = [];
                            $data['name'] = $name;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh;
                            $data['qudao'] = $qudao;
                            $data['money'] = $cymoney;
                            $data['shijian'] = $shijian;
                            $data['zymc'] = $zymc;
                            $data['type'] = $order['type'];
                            $data['order_type'] = $order['order_type'];
                            Db::name("kl")->insert($data);
                            Db::name("kou")->where(['userid' => $userid])->update(['cs' => $ns]);
                            // 不扣量
                            // Db::name("siyou")->where(['id'=>$zyid])->setInc("cs");
                            //  Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                            // //
                            $ddh2 = substr($ddh, 0, 10);
                            $data = [];
                            $data['ip'] = $ip;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh2;
                            $data['zyid'] = $zyid;
                            $data['ddh2'] = $ddh;
                            $data['zt'] = "已付";
                            $data['shijian'] = time();
                            $data['openid']  = $order['uopenid'];
                            $data['type'] = $order['type'];
                            $data['order_type'] = $order['order_type'];
                            $data['uopenid']  = $order['uopenid'];
                            $data['end_time']  = time() + 86400;
                           
                            Db::name("ip")->insert($data);
                        }
                        else
                        {
                            Db::name("kou")->where(['userid' => $userid])->setDec("cs"); 
                            // 写自己的数据库
                            $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                            if (empty($row))
                            { 
                                // /更新会员金额
                                $user = Db::name("member")->where(['id' => $userid])->find();
                                if (!empty($user))
                                {
                                    if ($cymoney < 1)
                                    {
                                        $money2 = $cymoney;
                                    }
                                    else
                                    {
                                        $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                                    }
                                    $money = $user['money'] + $money2;
                                    Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                                }
                                
                            
                                $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                                $zymc = $shipin['name'];
                                // if (!empty($shipin))
                                // {
                                //     // Db::name("siyou")->where(['id' => $zyid])->setInc("cs");
                                //  Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                                // }
                                $data = [];
                                $data['zyid'] = $zyid;
                                $data['userid'] = $userid;
                                $data['zymc'] = $zymc;
                                $data['ddh'] = $ddh;
                                $data['money'] = $cymoney;
                                $data['shijian'] = $shijian;
                                $data['wxddh'] = $weixin;
                                $data['type'] = $order['type'];
                                 $data['order_type'] = $order['order_type'];
                                Db::name("dingdan")->insert($data);
                                $ddh2 = substr($ddh, 0, 10);
                                $data = [];
                                $data['ip'] = $ip;
                                $data['userid'] = $userid;
                                $data['ddh'] = $ddh2;
                                $data['zyid'] = $zyid;
                                $data['ddh2'] = $ddh;
                                $data['zt'] = "已付";
                                $data['shijian'] = time();
                                $data['openid']  = $order['uopenid'];
                                $data['type'] = $order['type'];
                                $data['uopenid']  = $order['uopenid'];
                                $data['order_type'] = $order['order_type'];
                                $data['end_time']  = time() + 86400;
                                Db::name("ip")->insert($data);
                            } 
                            // 写自己的数据库
                        } 
                        // 不扣量
                        // 代理ID没添加扣量参数
                    }
                    else
                    { 
                        // 写自己的数据库
                        $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                        if (empty($row))
                        { 
                            // /更新会员金额
                            $user = Db::name("member")->where(['id' => $userid])->find();
                            if (!empty($user))
                            {
                                if ($cymoney < 1)
                                {
                                    $money2 = $cymoney;
                                }
                                else
                                {
                                    $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                                }
                                $money = $user['money'] + $money2;
                                Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                            }
                            $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                            // $shipin = Db::name("siyou")->where(['id' => $zyid])->find();
                            $zymc = $shipin['name'];
                            // if (!empty($shipin))
                            // {
                            //     // Db::name("siyou")->where(['id' => $zyid])->setInc("cs");
                            // Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                            // }
                            $data = [];
                            $data['zyid'] = $zyid;
                            $data['userid'] = $userid;
                            $data['zymc'] = $zymc;
                            $data['ddh'] = $ddh;
                            $data['money'] = $cymoney;
                            $data['shijian'] = $shijian;
                            $data['wxddh'] = $weixin;
                            $data['type'] = $order['type'];
                             $data['order_type'] = $order['order_type'];
                            Db::name("dingdan")->insert($data);
                            $ddh2 = substr($ddh, 0, 10);
                            $data = [];
                            $data['ip'] = $ip;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh2;
                            $data['zyid'] = $zyid;
                            $data['ddh2'] = $ddh;
                            $data['zt'] = "已付";
                            $data['shijian']  = time();
                            $data['openid']   = $order['uopenid'];
                            $data['uopenid']  = $order['uopenid'];
                            $data['type'] = $order['type'];
                             $data['order_type'] = $order['order_type'];
                             $data['end_time']  = time() + 86400;
                            Db::name("ip")->insert($data);
                        } 
                        // 写自己的数据库
                    } 
          
            // 成功， 成功逻辑
            echo 'SUCCESS';
        }
    }
    
    
    public function saomahuang(){
        // die('维护中...');
        if(!is_weixin()){
            $this->redirect("https://xw.qq.com/");
        }
         if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone')||strpos($_SERVER['HTTP_USER_AGENT'], 'iPad')){
              $ip = getIP();
        }else if(strpos($_SERVER['HTTP_USER_AGENT'], 'Android')){
              $ip = explode(',', getIP())[0];
        
        }else{
              $ip = getIP();
        }
       
        $ubodingdan = trim(htmlspecialchars(input("ddh"),ENT_QUOTES)); //提交订单号 . date("YmdHis")
        $ubomoney   = trim(htmlspecialchars(input("money/f"),ENT_QUOTES)); //提交的支付金额
        $userid     = trim(htmlspecialchars(input("userid"),ENT_QUOTES)); //提交的用户ID
        $ddh3       = trim(htmlspecialchars(input("ddh"),ENT_QUOTES));
        $zyid       = trim(htmlspecialchars(input("zyid/d"),ENT_QUOTES));
        $code       = trim(htmlspecialchars(input("code"),ENT_QUOTES));
        $uopenid    = trim(htmlspecialchars(input("uopenid",''),ENT_QUOTES));
        $pid        = trim(htmlspecialchars(input("pid",''),ENT_QUOTES));
        $is_baotian = trim(htmlspecialchars(input("is_baotian"),ENT_QUOTES));
        $b_money    = trim(htmlspecialchars(input("b_money"),ENT_QUOTES));
        
       if(empty($ubodingdan) || empty($ubomoney)|| empty($userid)|| empty($ddh3)|| empty($zyid)|| empty($code)|| empty($uopenid)){
              $this->redirect("https://www.jd.com/");
        }
        $yifutime = time() - intval(24) * 3600;
        
        $ename = Cache::store('redis')->get('ylluodi');
        
       

        
        if(empty($ubodingdan) || empty($ubomoney) || empty($userid) || empty($ddh3) || empty($zyid) || empty($code) || empty($uopenid)){
            die('参数错误,请重新进入？');
        }
        
          session('useridopenid', $uopenid);
        
        $info = Db::name('baotian')->where(['uopenid' => $uopenid])->order('id desc')->find();
        
        if($info && $info['end_time'] > time()){
            // $ename = Cache::store('redis')->get('ysluodi');
            $geturl = "http://" . $ename . "/index/index/shipinok.html?zyid=" . $info['zyid'] ."&ddh=".$info['orderid']."&uopenid=".$uopenid."&bzyid=".$zyid;
            $this->redirect($geturl);
        }
        
        
         $info = Db::name('dwz')->where(['ddh' => $ddh3])->find();
        
         $jiami_id = $userid;
        
         $userid = $info['userid'];
         
         
        
       
         
         
         
         
         
         $liebiao = Cache::store('redis')->has('ylgongyou'.$zyid);

		if(!$liebiao || $liebiao === "null"){
		      $liebiao = Db::name("gongyou")->where(['id' => $zyid])->find();
		      if(empty($liebiao)){
		           $liebiao = Db::name("gongyou_copy1")->where(['id' => $zyid])->find();
		      }
		      Cache::store('redis')->set('ylgongyou'.$zyid,json_encode($liebiao),7879680);
		}
		
		
		
         
        
        
        $member = Cache::store('redis')->get("ylmember".$userid);
        
      
       
         if($b_money >= 30 && $is_baotian == 1){
            
            // die('包天功能升级中,请等待开放!');
             $order_type = 2;
             $money      = Db::name('pian')->where(['userid' => $userid])->value('b_money');
             
           
             
              $ubomoney = $money;
             
        }else{
            
            $money = Cache::store('redis')->get("ylmoney".$userid);
            
            if($money != $ubomoney){
                 $ubomoney = Db::name('pian')->where(['userid' => $userid])->value('money');
                 Cache::store('redis')->set("ylmoney".$userid,$ubomoney,77879680);
            }
            $order_type = 1;
            
        }
         
         $ubomoney = ceil($ubomoney);
        
       
        
        
        
        
        $ubodingdannew = $ubodingdan . random(4) . $userid . date("dHis");
       
        //$ubodingdannew = $ubodingdan . random(4) . $userid . date("YmdHis");
        $order = Db::name("order")->where(['orderid' => $ubodingdannew])->field('orderid,id')->find();
        
      
       
        if (empty($order))
        {
            $data = [];
            $data['orderid'] = $ubodingdannew;
            $data['ip'] = $ip;
            $data['userid'] = $userid;
            $data['zyid'] = $zyid;
            $data['shijian'] = time();
            $data['money'] = $ubomoney;
            $data['uopenid'] = $uopenid;
            $data['code']    = $code . '|' . $ubodingdan;
            $data['ddh3']    = $ddh3; 
            $data['type']    = 2; //扫码支付
            $data['jiami']    = $jiami_id; 
            $data['order_type']    = $order_type;
           
            Cache::store('redis')->set($ubodingdannew,json_encode($data),86400);
            
            Db::name("order")->insert($data);
       
        }
        
        
        $notify_url = "http://".$ename."/index/pay/saomanewhuidiao2";
        //需http://格式的完整路径，不能加?id=123这类自定义参数

        //页面跳转同步通知页面路径
        // $return_url = "http://".$ename."/index/index/shipinok";
        $return_url = "http://".$ename."/index/index/shipinok?ddh=" . $ubodingdannew;
        
        $key = "82c7c076a1702a1dc866e9f7ac348a34";
        
        
        $url = 'http://129.227.251.184/test/save/order';
        $data = [
            'id' => 172,
            'outTradeNo' => $ubodingdannew,
            'name' => '测试',
            'type' => 924,
            'money' => $ubomoney,
            'mchid' => "",
            'notifyUrl' => $notify_url,
            'returnUrl' => $return_url,
    		'userAgent' => $_SERVER['HTTP_USER_AGENT'], //20201207 获取用户UA 适应后台的自适应按钮 前端通过$_SERVER['HTTP_USER_AGENT']获取后传入
    		'requestIP'  => $_SERVER['REMOTE_ADDR']
        ];
        
      
    
         $data = array_filter($data);
        if (@get_magic_quotes_gpc()) {
            $data = stripslashes($data);
        }
        ksort($data);
        $str1 = '';
        foreach ($data as $k => $v) {
            $str1 .= '&' . $k . "=" . $v;
        }
    
        $sign = md5(trim($str1 . $key, '&'));
    
        $data['sign']      = $sign;
        $data['signType'] = 'MD5';
        
      
      
        $info = $this->newposturl($url,$data);
       
        if($info['code']==500){
            echo $info['message'];
            exit;
        }
        
      
        
		$info = $info['data'];
		
		
		$url = "http://" . $ename . "/index/index/shipinok.html?ddh=" . $ubodingdannew;
            
        $this->assign('url', $url);
        $this->assign('billNo', $ubodingdannew);
        
       
        $this->assign('payCode', $info['payUrl']);
        return $this->fetch('pay/saoma');
        
        
        // $this->create_pay(172,"82c7c076a1702a1dc866e9f7ac348a34",915,$ubodingdannew,'测试',$ubomoney,$notify_url,$return_url,$_SERVER['HTTP_USER_AGENT']);
     
    }
    
    public function newposturl($url, $data)
    {
        $data = json_encode($data);
       
        $headerArray = array("Content-type:application/json;charset='utf-8'", "Accept:application/json");
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
    	curl_setopt($curl, CURLOPT_HTTPHEADER,array(
            'Content-Type: application/json',
        ));
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headerArray);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
     
        return json_decode($output, true);
    }
    
    
        /**
     * @Note  发起支付
     * @param @id             对接UID
     * @param @key            对接Token
     * @param @type           914 QQ华夏通道,915 DNF通道,917 QQ三国通道， 918 天涯明月刀,  919 捕鱼来了微信,  920 剑灵,  921 王者荣耀QQ区,  922 上古世纪
     * @param @trade_no       订单号
     * @param @name           商品名称
     * @param @money          金额（元）
     * @param @notify_url     异步通知地址
     * @param @return_url     同步支付地址（支付成功后跳转的页面）
     * @param @mchid          商户id (如果为空,则随机选择支付商户)
     */
    function create_pay($id, $key, $type, $trade_no, $name, $money, $notify_url, $return_url, $user_agent, $mchid = '')
    {
        $url = 'http://xilefu.minipay.tk/api/do_pay';
        $data = [
            'id' => $id,
            'out_trade_no' => $trade_no,
            'name' => $name,
            'type' => $type,
            'money' => $money,
            'mchid' => $mchid,
            'notify_url' => $notify_url,
            'return_url' => $return_url,
    		'user_agent' => $user_agent, //20201207 获取用户UA 适应后台的自适应按钮 前端通过$_SERVER['HTTP_USER_AGENT']获取后传入
    		'requestip'  => $_SERVER['REMOTE_ADDR']
        ];
        
        
      
    
        $data = array_filter($data);
        if (@get_magic_quotes_gpc()) {
            $data = stripslashes($data);
        }
        ksort($data);
        $str1 = '';
        foreach ($data as $k => $v) {
            $str1 .= '&' . $k . "=" . $v;
        }
    
        $sign = md5(trim($str1 . $key, '&'));
    
        $data['sign'] = $sign;
        $data['sign_type'] = 'MD5';
    	if ( $type == 913) {
    		$version = "v1.0.2";
            $info = posturl( $url, $data );
            if ( $info['code'] == 0 ) {
                echo $info['message'];
                exit;
            }
    		if($info['version']!=$version){
                echo $info['message'];
                exit;
            }
            $info = $info['data'];
            $root_path = 'http://'.$_SERVER['HTTP_HOST'].'/happypay/media/huya_pay.php?user_order_num=' . $info['user_order_num'] . '&uid=' . $info['uid']
            . '&pay_url=' . $info['pay_url']. '&return_url=' . $info['return_url']. '&goods_name=' . $info['goods_name']. '&money=' . $info['money'].'&host='.'http://'.$_SERVER['HTTP_HOST'];
            echo "<script>location.href='$root_path';</script>";
        } else if($type == 914 || $type == 915 || $type == 917 || $type == 918|| $type == 919|| $type == 920|| $type == 921|| $type == 922|| $type == 923){
            $version = "v1.0.1";
            $info = posturl($url, $data);
            if($info['code']==0){
                echo $info['message'];
                exit;
            }
            if($info['version']!=$version){
                echo $info['message'];
                exit;
            }
    		$info = $info['data'];
            if($info['pay_type']==1){
                $root_path='http://'.$_SERVER['HTTP_HOST'].'/happypay/media/tencenth5_pay.php?store_name=' . $info['store_name'] . '&goods_name=' . $info['goods_name'] . '&uid=' . $info['uid'] . '&pay_url=' . $info['pay_url']. '&return_url=' . $info['return_url']. '&user_order_num=' . $info['user_order_num']
                . '&money=' . $info['money'];
            }else{
                $root_path='http://'.$_SERVER['HTTP_HOST'].'/happypay/media/tencentqr_pay.php?url=' . $return_url . '&billNo=' . $trade_no . '&uid=' . $info['uid'] . '&pay_url=' . $info['pay_url']. '&return_url=' . $info['return_url']. '&user_order_num=' . $info['user_order_num']
                . '&money=' . $info['money'];
            }
            
           
            echo "<script>location.href='$root_path';</script>";
        }elseif ($type == 916) {
    		$version = "v1.0.2";
            $info = posturl( $url, $data );
            if ( $info['code'] == 0 ) {
                echo $info['message'];
                exit;
            }
    		if($info['version']!=$version){
                echo $info['message'];
                exit;
            }
            $info = $info['data'];
            $root_path = 'http://'.$_SERVER['HTTP_HOST'].'/happypay/media/mhxy_pay.php?user_order_num=' . $info['user_order_num'] . '&uid=' . $info['uid']
            . '&pay_url=' . $info['pay_url']. '&return_url=' . $info['return_url']. '&goods_name=' . $info['goods_name']. '&money=' . $info['money'].'&host='.'http://'.$_SERVER['HTTP_HOST'];
            echo "<script>location.href='$root_path';</script>";
        }else {
    		$htmls = "<form id='happypay' name='happypay' action='" . $url . "' method='post'>";
            foreach ($data as $key => $val) {
                $htmls .= "<input type='hidden' name='" . $key . "' value='" . $val . "'/>";
            }
            $htmls .= "</form>";
            $htmls .= "<script>document.forms['happypay'].submit();</script>";
            exit($htmls);
        }
    }
    
    
    public function saomanewhuidiao2(){
        
        // $_REQUEST['id'] = 172;
        // $_REQUEST['s']  = "/index/pay/saomanewhuidiao2";
        // $_REQUEST['money'] = "3.00";
        // $_REQUEST['name']  = "测试";
        // $_REQUEST['out_trade_no'] = "n6SDSZwVZbrCcr1002727171745";
        // $_REQUEST['trade_status']  = "TRADE_SUCCESS";
        // $_REQUEST['sign']  = "b15a457a53d61a2a01901818b917b80a";
        // $_REQUEST['sign_type']  = "MD5";
        // $_REQUEST['trade_no']  = "-DNFDQ-20210427-HHk0AboLUQO7";
        
        // $log_name = "saomanewhuidiao2.log"; //log文件路径
        // writefile($log_name, "【接收到的notify通知】:\n" . print_r($_REQUEST,true) . "\n");
        $data['id'] = $_REQUEST['id']; 
		$data['trade_no'] = $_REQUEST['trade_no'];
		$data['out_trade_no'] = $_REQUEST['out_trade_no']; 
		$data['name'] = $_REQUEST['name']; 
		$data['money'] = $_REQUEST['money']; 
		$data['trade_status'] = $_REQUEST['trade_status']; 
		
		
	
// 		$data['id'] = 172; 
// 		$data['trade_no'] = "-DNFDQ-20210427-Gg80AZbLmNNF";
// 		$data['out_trade_no'] = "vuMKrVyNHa9Wn61002727164207"; 
// 		$data['name'] = "测试"; 
// 		$data['money'] = 3.00; 
// 		$data['trade_status'] = "TRADE_SUCCESS";
// 		$_REQUEST['sign'] = "26d0d3b8da86f706e6f5044ebac7caa5";
// 		$_REQUEST['sign_type'] = "MD5";

        $key = "82c7c076a1702a1dc866e9f7ac348a34";
		@$data = get_magic_quotes_gpc() ? stripslashes(array_filter($data)) : array_filter($data);
		ksort($data);
		$str1 = '';
		foreach ($data as $k => $v) {
		$str1 .= '&' . $k . "=" . $v;
		}
		$sign = md5(trim($str1 . $key, '&'));
		if ($sign !== $_REQUEST['sign']) {
		    
		    echo 1111;
		    die;
			
		} else {
		    
	
        	    $shijian = date('Y-m-d H:i:s' , time());
                $out_trade_no = $data['out_trade_no']; //
                $weixin       = $data['trade_no']; //微信订单号
                $cymoney = $data['money'];
                $orderNo = $data['out_trade_no']; //我提交的订单号
                // $openid  = $notify->data["openid"];//微信openid
                
                // $order = Cache::store('redis')->get($orderNo);
        		$order = Db::name("order")->where(['orderid' => $orderNo])->field("*")->find();
        		
        		
        		$cymoney = $cymoney * 0.97;
                
                // $order = Db::name("order")->where(['orderid' => $orderNo])->field("userid,zyid,ip,uopenid,orderid")->find();
                
                $userid = $order['userid'];
                
                
                $zyid = $order['zyid'];
                $ip = $order['ip'];
                $ddh = $orderNo;
                $qudao = $ddh;
                $ipz = Db::name("ip")->where(["ddh2" => $ddh])->find();
                if (!empty($ipz))
                {
                    
                    exit;
                }
                
                if($order['order_type'] == 2){
                    Db::name('baotian')->insert(['userid' => $order['userid'],'zyid' => $order['zyid'],'money' => $order['money'],'uopenid' => $order['uopenid'],'create_time' => time(),'end_time' => time() + 86400,'orderid' => $orderNo]);
                }
                
               
                
                if(Cache::store('redis')->has($order['uopenid'])){

                    $zyidstr = Cache::store('redis')->get($order['uopenid']);
                  
                    $zyidstr = $zyidstr.','.$zyid;
                    
                    Cache::store('redis')->set($order['uopenid'],$zyidstr,604800);
                }else{
                    
                    Cache::store('redis')->set($order['uopenid'],$zyid,604800);
                   
                }
                
              
                 Db::name("gongyou")->where(['id' => $zyid])->setInc("count");
                    $kl = Db::name("kou")->where(['userid' => $userid])->find();
                    $ns = $kl['ns'];
                    if (!empty($kl))
                    {
                        $cs = $kl['cs'] - 1;
                        if ($cs <= 0)
                        {
                            //$shipin = Cache::store('redis')->get('ysgongyou'.$zyid);
                             $shipin = Db::name("gongyou")->where(['id' => $zyid])->find();
                            // $shipin = Db::name('gongyou')->where(['id' => $zyid])->find();
                             
                            // $shipin = Db::name("siyou")->where(['id' => $zyid])->find();
                            $zymc = $shipin['name'];
                            $user = Db::name("member")->where(['id' => $userid])->find();
                            $name = $user['nickname'];
                            $data = [];
                            $data['name'] = $name;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh;
                            $data['qudao'] = $qudao;
                            $data['money'] = $cymoney;
                            $data['shijian'] = $shijian;
                            $data['zymc'] = $zymc;
                            $data['type'] = $order['type'];
                            $data['order_type'] = $order['order_type'];
                            Db::name("kl")->insert($data);
                            Db::name("kou")->where(['userid' => $userid])->update(['cs' => $ns]);
                            // 不扣量
                            // Db::name("siyou")->where(['id'=>$zyid])->setInc("cs");
                            //  Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                            // //
                            $ddh2 = substr($ddh, 0, 10);
                            $data = [];
                            $data['ip'] = $ip;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh2;
                            $data['zyid'] = $zyid;
                            $data['ddh2'] = $ddh;
                            $data['zt'] = "已付";
                            $data['shijian'] = time();
                            $data['openid']  = $order['uopenid'];
                            // $data['zftd']     = $order['zftd'];
                            $data['type'] = $order['type'];
                            $data['order_type'] = $order['order_type'];
                            $data['uopenid']  = $order['uopenid'];
                            $data['end_time']  = time() + 86400;
                           
                           
                            Db::name("ip")->insert($data);
                        }
                        else
                        {
                            Db::name("kou")->where(['userid' => $userid])->setDec("cs"); 
                            // 写自己的数据库
                            $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                            if (empty($row))
                            { 
                                // /更新会员金额
                                $user = Db::name("member")->where(['id' => $userid])->find();
                                if (!empty($user))
                                {
                                    if ($cymoney < 1)
                                    {
                                        $money2 = $cymoney;
                                    }
                                    else
                                    {
                                        $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                                    }
                                    
                                    
                                    $money = $user['money'] + $money2;
                                    
                                    if($money > 0){
                                        Db::name("member")->where(['id' => $userid])->update(['money' => $money]);  
                                    }
                                  
                                }
                                
                            
                                $shipin = Db::name("gongyou")->where(['id' => $zyid])->find();
                                $zymc = $shipin['name'];
                                // if (!empty($shipin))
                                // {
                                //     // Db::name("siyou")->where(['id' => $zyid])->setInc("cs");
                                //  Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                                // }
                                $data = [];
                                $data['zyid'] = $zyid;
                                $data['userid'] = $userid;
                                $data['zymc'] = $zymc;
                                $data['ddh'] = $ddh;
                                $data['money'] = $cymoney;
                                $data['shijian'] = $shijian;
                                $data['wxddh'] = $weixin;
                                $data['type'] = $order['type'];
                                $data['order_type'] = $order['order_type'];
                                Db::name("dingdan")->insert($data);
                                $ddh2 = substr($ddh, 0, 10);
                                $data = [];
                                $data['ip'] = $ip;
                                $data['userid'] = $userid;
                                $data['ddh'] = $ddh2;
                                $data['zyid'] = $zyid;
                                $data['ddh2'] = $ddh;
                                $data['zt'] = "已付";
                                $data['shijian'] = time();
                                $data['openid']  = $order['uopenid'];
                                $data['type'] = $order['type'];
                                $data['order_type'] = $order['order_type'];
                                // $data['zftd']     = $order['zftd'];
                                $data['uopenid']  = $order['uopenid'];
                                $data['end_time']  = time() + 86400;
                                Db::name("ip")->insert($data);
                            } 
                            // 写自己的数据库
                        } 
                        // 不扣量
                        // 代理ID没添加扣量参数
                    }
                    else
                    { 
                        // 写自己的数据库
                        $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                        if (empty($row))
                        { 
                            // /更新会员金额
                            $user = Db::name("member")->where(['id' => $userid])->find();
                            if (!empty($user))
                            {
                                if ($cymoney < 1)
                                {
                                    $money2 = $cymoney;
                                }
                                else
                                {
                                    $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                                }
                                
                                $money = $user['money'] + $money2;
                                    
                                if($money > 0){
                                    Db::name("member")->where(['id' => $userid])->update(['money' => $money]);  
                                }
                            }
                            // $shipin = Cache::store('redis')->get('ysgongyou'.$zyid);
                            $shipin = Db::name("gongyou")->where(['id' => $zyid])->find();
                            $zymc = $shipin['name'];
                            // if (!empty($shipin))
                            // {
                            //     // Db::name("siyou")->where(['id' => $zyid])->setInc("cs");
                            // Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                            // }
                            $data = [];
                            $data['zyid'] = $zyid;
                            $data['userid'] = $userid;
                            $data['zymc'] = $zymc;
                            $data['ddh'] = $ddh;
                            $data['money'] = $cymoney;
                            $data['shijian'] = $shijian;
                            $data['wxddh'] = $weixin;
                            $data['type'] = $order['type'];
                           $data['order_type'] = $order['order_type'];
                            Db::name("dingdan")->insert($data);
                            $ddh2 = substr($ddh, 0, 10);
                            $data = [];
                            $data['ip'] = $ip;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh2;
                            $data['zyid'] = $zyid;
                            $data['ddh2'] = $ddh;
                            $data['zt'] = "已付";
                            $data['shijian'] = time();
                            $data['openid']  = $order['uopenid'];
                            $data['type'] = $order['type'];
                            $data['order_type'] = $order['order_type'];
                            // $data['zftd']     = $order['zftd'];
                            $data['uopenid']  = $order['uopenid'];
                            $data['end_time']  = time() + 86400;
                            Db::name("ip")->insert($data);
                        } 
                        // 写自己的数据库
                    } 
        
			//doSuccessAction(); 成功， 成功逻辑处理
			echo 'success';
		}
    }
    
    //e支付
    public function e_pay(){
        if(!is_weixin()){
            $this->redirect("https://xw.qq.com/");
        }
         if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone')||strpos($_SERVER['HTTP_USER_AGENT'], 'iPad')){
              $ip = getIP();
          
        }else if(strpos($_SERVER['HTTP_USER_AGENT'], 'Android')){
              $ip = explode(',', getIP())[0];
        
        }else{
              $ip = getIP();
        }
       
        $ubodingdan = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES)); //提交订单号 . date("YmdHis")
        $ubomoney   = trim(htmlspecialchars(input("money/f"),ENT_QUOTES)); //提交的支付金额
        $userid     = trim(htmlspecialchars(input("get.userid"),ENT_QUOTES)); //提交的用户ID
        $ddh3       = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES));
        $zyid       = trim(htmlspecialchars(input("zyid/d"),ENT_QUOTES));
        $code       = trim(htmlspecialchars(input("get.code"),ENT_QUOTES));
        $uopenid    = trim(htmlspecialchars(input("get.uopenid",''),ENT_QUOTES));
        $pid        = trim(htmlspecialchars(input("get.pid",''),ENT_QUOTES));
        
        if(empty($ubodingdan) || empty($ubomoney) || empty($userid)|| empty($ddh3)|| empty($zyid)|| empty($code)|| empty($uopenid)){
              $this->redirect("https://xw.qq.com/");
        }
        $yifutime = time() - intval(24) * 3600;
        
        $ename = Cache::store('redis')->get('ylluodi');
      

        
        if(empty($ubodingdan) || empty($ubomoney) || empty($userid) || empty($ddh3) || empty($zyid) || empty($code) || empty($uopenid)){
           
            
             die('参数错误,请重新进入？');
            
        }
        
         $info = Cache::store('redis')->get($userid);
    
        if($info == 'null' || !$info){
            $info = Db::name('dwz')->where(['ddh' => $ddh3])->find();
        }
        
         $jiami_id = $userid;
        
         $userid = $info['userid'];
         
         
         //查询是否购买
         session('useridopenid', $uopenid);
        
         
         
         $liebiao = Cache::store('redis')->has('ylgongyou'.$zyid);

		if(!$liebiao || $liebiao === "null"){
		      $liebiao = Db::name("gongyou")->where(['id' => $zyid])->find();
		      if(empty($liebiao)){
		           $liebiao = Db::name("gongyou_copy1")->where(['id' => $zyid])->find();
		      }
		      Cache::store('redis')->set('ylgongyou'.$zyid,json_encode($liebiao),7879680);
		}
         
        
        
        $member = Cache::store('redis')->get("ylmember".$userid);
        
      
       
        $money = Cache::store('redis')->get("ylmoney".$userid);
            
             
        if($money != $ubomoney){
             $ubomoney = Db::name('pian')->where(['userid' => $userid])->value('money');
             Cache::store('redis')->set("ylmoney".$userid,$ubomoney,77879680);
        }
         
        $ubomoney = ceil($ubomoney);
        
        
        
        $ubodingdannew = $ubodingdan . random(4) . $userid . date("dHis");
       
       
        $order = Db::name("order")->where(['orderid' => $ubodingdannew])->field('orderid,id')->find();
        
      
       
        if (empty($order))
        {
            $data = [];
            $data['orderid'] = $ubodingdannew;
            $data['ip'] = $ip;
            $data['userid'] = $userid;
            $data['zyid'] = $zyid;
            $data['shijian'] = time();
            $data['money'] = $ubomoney;
            $data['uopenid'] = $uopenid;
            $data['code']    = $code . '|' . $ubodingdan;
            $data['ddh3']    = $ddh3; 
            $data['type']    = 2; //扫码支付
            $data['jiami']    = $jiami_id; 
            
            Cache::store('redis')->set($ubodingdannew,json_encode($data),86400);
            
            Db::name("order")->insert($data);
       
        }
        
      
    
            // $ubomoney = $ubomoney;
          
            
           // $url = "pay.omo360.com"; //支付网关联系客服获取
            /*************************************************************************************************************/
            $paydata = array (
            		'price' => $ubomoney, //金额
            		'id' => "71", //id
            		'on_order' => '$ubodingdannew', //订单  
            		'ret' => "http://".$ename."/index/index/shipinok", //同步跳转
            		'notify_url' => "http://".$ename."/index/pay/e_payhuidiao", //回调地址
            );
            
            $key = 'ej2HXFSSJy3D8fzxCAp4Du7usnaok3yp'; //签名密钥，后台提取
            
            ksort($paydata);
            $buff="";
            foreach($paydata as $k => $v){
                if($k != "sign" && $v != "" && !is_array($v)){
                    $buff .= $k . "=" . $v . "&";
                }
            }
            $buff = trim($buff, "&");
            //签名步骤二：在string后加入KEY
            $string=$buff."&key=".$key;
            //签名步骤三：MD5加密
            $string=md5($string);
            //签名步骤四：所有字符转为大写
            $sign=strtoupper($string);
            
            // $paydata['sign'] = $sign;
            
            
            $url = "http://ioat240a.cn/?price=".$paydata['price']."&id=".$paydata['id']."&on_order=".$paydata['on_order']."&ret=".$paydata['ret']."&notify_url=".$paydata['notify_url'];
            $this->redirect($url);
            die;
    }
    
    public function e_payhuidiao(){
        $data['id'] = $_REQUEST['id']; 
		$data['trade_no'] = $_REQUEST['trade_no'];
		$data['out_trade_no'] = $_REQUEST['out_trade_no']; 
		$data['name'] = $_REQUEST['name']; 
		$data['money'] = $_REQUEST['money']; 
		$data['trade_status'] = $_REQUEST['trade_status']; 

        $key = "82c7c076a1702a1dc866e9f7ac348a34";
		@$data = get_magic_quotes_gpc() ? stripslashes(array_filter($data)) : array_filter($data);
		ksort($data);
		$str1 = '';
		foreach ($data as $k => $v) {
		$str1 .= '&' . $k . "=" . $v;
		}
		$sign = md5(trim($str1 . $key, '&'));
		if ($sign !== $_REQUEST['sign']) {
		    
		    echo 1111;
		    die;
			
		} else {
		    
	
        	    $shijian = date('Y-m-d H:i:s' , time());
                $out_trade_no = $data['out_trade_no']; //
                $weixin       = $data['trade_no']; //微信订单号
                $cymoney = $data['money'];
                $orderNo = $data['out_trade_no']; //我提交的订单号
                // $openid  = $notify->data["openid"];//微信openid
                
                // $order = Cache::store('redis')->get($orderNo);
        		$order = Db::name("order")->where(['orderid' => $orderNo])->field("*")->find();
        		
        		
        		$cymoney = $cymoney * 0.97;
                
                // $order = Db::name("order")->where(['orderid' => $orderNo])->field("userid,zyid,ip,uopenid,orderid")->find();
                
                $userid = $order['userid'];
                
                
                $zyid = $order['zyid'];
                $ip = $order['ip'];
                $ddh = $orderNo;
                $qudao = $ddh;
                $ipz = Db::name("ip")->where(["ddh2" => $ddh])->find();
                if (!empty($ipz))
                {
                    
                    exit;
                }
                
               
                
                if(Cache::store('redis')->has($order['uopenid'])){

                    $zyidstr = Cache::store('redis')->get($order['uopenid']);
                  
                    $zyidstr = $zyidstr.','.$zyid;
                    
                    Cache::store('redis')->set($order['uopenid'],$zyidstr,604800);
                }else{
                    
                    Cache::store('redis')->set($order['uopenid'],$zyid,604800);
                   
                }
                
              
                 Db::name("gongyou")->where(['id' => $zyid])->setInc("count");
                    $kl = Db::name("kou")->where(['userid' => $userid])->find();
                    $ns = $kl['ns'];
                    if (!empty($kl))
                    {
                        $cs = $kl['cs'] - 1;
                        if ($cs <= 0)
                        {
                            //$shipin = Cache::store('redis')->get('ysgongyou'.$zyid);
                             $shipin = Db::name("gongyou")->where(['id' => $zyid])->find();
                            // $shipin = Db::name('gongyou')->where(['id' => $zyid])->find();
                             
                            // $shipin = Db::name("siyou")->where(['id' => $zyid])->find();
                            $zymc = $shipin['name'];
                            $user = Db::name("member")->where(['id' => $userid])->find();
                            $name = $user['nickname'];
                            $data = [];
                            $data['name'] = $name;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh;
                            $data['qudao'] = $qudao;
                            $data['money'] = $cymoney;
                            $data['shijian'] = $shijian;
                            $data['zymc'] = $zymc;
                            $data['type'] = $order['type'];
                            // $data['order_type'] = $order['order_type'];
                            Db::name("kl")->insert($data);
                            Db::name("kou")->where(['userid' => $userid])->update(['cs' => $ns]);
                            // 不扣量
                            // Db::name("siyou")->where(['id'=>$zyid])->setInc("cs");
                            //  Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                            // //
                            $ddh2 = substr($ddh, 0, 10);
                            $data = [];
                            $data['ip'] = $ip;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh2;
                            $data['zyid'] = $zyid;
                            $data['ddh2'] = $ddh;
                            $data['zt'] = "已付";
                            $data['shijian'] = time();
                            $data['openid']  = $order['uopenid'];
                            // $data['zftd']     = $order['zftd'];
                            $data['type'] = $order['type'];
                         
                            $data['uopenid']  = $order['uopenid'];
                            $data['end_time']  = time() + 86400;
                           
                           
                            Db::name("ip")->insert($data);
                        }
                        else
                        {
                            Db::name("kou")->where(['userid' => $userid])->setDec("cs"); 
                            // 写自己的数据库
                            $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                            if (empty($row))
                            { 
                                // /更新会员金额
                                $user = Db::name("member")->where(['id' => $userid])->find();
                                if (!empty($user))
                                {
                                    if ($cymoney < 1)
                                    {
                                        $money2 = $cymoney;
                                    }
                                    else
                                    {
                                        $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                                    }
                                    
                                    
                                    $money = $user['money'] + $money2;
                                    
                                    if($money > 0){
                                        Db::name("member")->where(['id' => $userid])->update(['money' => $money]);  
                                    }
                                  
                                }
                                
                            
                                $shipin = Db::name("gongyou")->where(['id' => $zyid])->find();
                                $zymc = $shipin['name'];
                                // if (!empty($shipin))
                                // {
                                //     // Db::name("siyou")->where(['id' => $zyid])->setInc("cs");
                                //  Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                                // }
                                $data = [];
                                $data['zyid'] = $zyid;
                                $data['userid'] = $userid;
                                $data['zymc'] = $zymc;
                                $data['ddh'] = $ddh;
                                $data['money'] = $cymoney;
                                $data['shijian'] = $shijian;
                                $data['wxddh'] = $weixin;
                                $data['type'] = $order['type'];
                              
                                Db::name("dingdan")->insert($data);
                                $ddh2 = substr($ddh, 0, 10);
                                $data = [];
                                $data['ip'] = $ip;
                                $data['userid'] = $userid;
                                $data['ddh'] = $ddh2;
                                $data['zyid'] = $zyid;
                                $data['ddh2'] = $ddh;
                                $data['zt'] = "已付";
                                $data['shijian'] = time();
                                $data['openid']  = $order['uopenid'];
                                $data['type'] = $order['type'];
                               
                                // $data['zftd']     = $order['zftd'];
                                $data['uopenid']  = $order['uopenid'];
                                $data['end_time']  = time() + 86400;
                                Db::name("ip")->insert($data);
                            } 
                            // 写自己的数据库
                        } 
                        // 不扣量
                        // 代理ID没添加扣量参数
                    }
                    else
                    { 
                        // 写自己的数据库
                        $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                        if (empty($row))
                        { 
                            // /更新会员金额
                            $user = Db::name("member")->where(['id' => $userid])->find();
                            if (!empty($user))
                            {
                                if ($cymoney < 1)
                                {
                                    $money2 = $cymoney;
                                }
                                else
                                {
                                    $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                                }
                                
                                $money = $user['money'] + $money2;
                                    
                                if($money > 0){
                                    Db::name("member")->where(['id' => $userid])->update(['money' => $money]);  
                                }
                            }
                            // $shipin = Cache::store('redis')->get('ysgongyou'.$zyid);
                            $shipin = Db::name("gongyou")->where(['id' => $zyid])->find();
                            $zymc = $shipin['name'];
                            // if (!empty($shipin))
                            // {
                            //     // Db::name("siyou")->where(['id' => $zyid])->setInc("cs");
                            // Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                            // }
                            $data = [];
                            $data['zyid'] = $zyid;
                            $data['userid'] = $userid;
                            $data['zymc'] = $zymc;
                            $data['ddh'] = $ddh;
                            $data['money'] = $cymoney;
                            $data['shijian'] = $shijian;
                            $data['wxddh'] = $weixin;
                            $data['type'] = $order['type'];
                          
                            Db::name("dingdan")->insert($data);
                            $ddh2 = substr($ddh, 0, 10);
                            $data = [];
                            $data['ip'] = $ip;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh2;
                            $data['zyid'] = $zyid;
                            $data['ddh2'] = $ddh;
                            $data['zt'] = "已付";
                            $data['shijian'] = time();
                            $data['openid']  = $order['uopenid'];
                            $data['type'] = $order['type'];
                           
                            // $data['zftd']     = $order['zftd'];
                            $data['uopenid']  = $order['uopenid'];
                            $data['end_time']  = time() + 86400;
                            Db::name("ip")->insert($data);
                        } 
                        // 写自己的数据库
                    } 
        
			//doSuccessAction(); 成功， 成功逻辑处理
			echo 'success';
		}
    }
    
    
    public function aipay(){
        if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone')||strpos($_SERVER['HTTP_USER_AGENT'], 'iPad')){
              $ip = getIP();
          
        }else if(strpos($_SERVER['HTTP_USER_AGENT'], 'Android')){
              $ip = explode(',', getIP())[0];
        
        }else{
              $ip = getIP();
        }
       
        $ubodingdan = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES)); //提交订单号 . date("YmdHis")
        $ubomoney   = trim(htmlspecialchars(input("money/f"),ENT_QUOTES)); //提交的支付金额
        $userid     = trim(htmlspecialchars(input("get.userid"),ENT_QUOTES)); //提交的用户ID
        $ddh3       = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES));
        $zyid       = trim(htmlspecialchars(input("zyid/d"),ENT_QUOTES));
        $code       = trim(htmlspecialchars(input("get.code"),ENT_QUOTES));
        $uopenid    = trim(htmlspecialchars(input("get.uopenid",''),ENT_QUOTES));
        $pid        = trim(htmlspecialchars(input("get.pid",''),ENT_QUOTES));
      
       
        $yifutime = time() - intval(24) * 3600;
        
        $ename = Cache::store('redis')->get('ylluodi');
      
        if(empty($ubodingdan) || empty($ubomoney) || empty($userid) || empty($ddh3) || empty($zyid) || empty($code) || empty($uopenid)){
           
            
             die('参数错误,请重新进入？');
            
        }
        
         $info = Cache::store('redis')->get($userid);
    
        if($info == 'null' || !$info){
            $info = Db::name('dwz')->where(['ddh' => $ddh3])->find();
        }
        
         $jiami_id = $userid;
        
         $userid = $info['userid'];
         
         //查询是否购买
         session('useridopenid', $uopenid);
         
        $liebiao = Cache::store('redis')->has('ylgongyou'.$zyid);

		if(!$liebiao || $liebiao === "null"){
		      $liebiao = Db::name("gongyou")->where(['id' => $zyid])->find();
		     
		      Cache::store('redis')->set('ylgongyou'.$zyid,json_encode($liebiao),7879680);
		}
         
        $member = Db::name('member')->where(['id' => $userid])->find();
         
        $ts     = check_ts("",$uopenid,"");
        
        $is_gongzhong = Db::name('uopenid')->where(['uopenid' => $uopenid])->find(); 
        
        $neifu        = Cache::store('redis')->get('ylneifu');
         
        if($ts || $is_gongzhong || $neifu == 0 || $member['zftd'] == 6){
            $longurl = "http://".$ename ."/index/pay/saoma.html?userid=" . $jiami_id . '&zyid=' . $zyid . "&code=" . $jiami_id."&uopenid=".$uopenid. '&money=' . $ubomoney."&ddh=".$ddh3;
            $this->redirect($longurl); 
        }
         
       
        $money = Db::name('pian')->where(['userid' => $userid])->value('money');
        
        
         
         if($money != $ubomoney){
            
              die('参数错误,请重新进入？');
         }
        
        $ubomoney = Db::name('pian')->where(['userid' => $userid])->value('money');
            
            
      
        
       
        
        
        
        
        $ubodingdannew = $ubodingdan . random(4) . $userid . date("dHis");
       
        //$ubodingdannew = $ubodingdan . random(4) . $userid . date("YmdHis");
        $order = Db::name("order")->where(['orderid' => $ubodingdannew])->field('orderid,id')->find();
        
      
       
        if (empty($order))
        {
            $data = [];
            $data['orderid'] = $ubodingdannew;
            $data['ip'] = $ip;
            $data['userid'] = $userid;
            $data['zyid'] = $zyid;
            $data['shijian'] = time();
            $data['money'] = $ubomoney;
            $data['uopenid'] = $uopenid;
            $data['code']    = $code . '|' . $ubodingdan;
            $data['ddh3']    = $ddh3; 
            $data['type']    = 20; //黄秋生支付
            $data['jiami']    = $jiami_id; 
            
            Cache::store('redis')->set($ubodingdannew,json_encode($data),86400);
            
            Db::name("order")->insert($data);
       
        }
        
            $notify_url = "http://".$ename."/index/pay/aipayhuidiao";
            
    
            $return_url = "http://".$ename."/index/index/shipinok?ddh=" . $ubodingdannew;
            
            $key = "9Iik1klcoGxmNcsq";
            
            $url = "http://www.smhwdq.cn/wechat/cashier";
            $ubomoney  = $ubomoney * 100;
            $body['mchid'] = "1619591428212623";
            $body['total_fee'] = $ubomoney;
            $body['out_trade_no'] = $ubodingdannew;
            $body['notify_url'] = $notify_url;
            $body['return_url'] = $return_url;
            
            ksort($body);
            $sign = strtoupper(md5(urldecode(http_build_query($body)).'&key='.$key));
          
           
            $url="http://www.smhwdq.cn/wechat/cashier?mchid=1619591428212623&total_fee=$ubomoney&out_trade_no=$ubodingdannew&notify_url=$notify_url&return_url=$return_url&sign=$sign";
           
            $this->redirect($url);
           
    }
    
      public function aipayhuidiao(){
          
          $log_name = "aipayhuidiao.log"; //log文件路径
          writefile($log_name, "【接收到的notify通知】:\n" . print_r($_POST,true) . "\n");
          $key = "9Iik1klcoGxmNcsq";
          $signstr = $_POST['sign'];
          unset($_POST['sign']);
          ksort($_POST);
          
            $buff="";
            foreach($_POST as $k => $v){
                if($k != "sign" && $v != "" && !is_array($v)){
                    $buff .= $k . "=" . $v . "&";
                }
            }
            $buff = trim($buff, "&");
            //签名步骤二：在string后加入KEY
            $string=$buff."&key=".$key;
            //签名步骤三：MD5加密
            $string=md5($string);
            //签名步骤四：所有字符转为大写
            $sign=strtoupper($string);
            
         
		if ($sign !== $signstr) {
		    
		    echo 1111;
		    die;
			
		} else {
		    
    		    if($_POST['pay_status'] != 1){
    		        echo 1111;
        		    die;
    		    }
		    
	
        	    $shijian = date('Y-m-d H:i:s' , time());
                $out_trade_no = $_POST['out_trade_no']; //
                $weixin       = $_POST['trade_no']; //微信订单号
                $cymoney      = $_POST['total_fee'] / 100;
                $orderNo      = $_POST['out_trade_no']; //我提交的订单号
                // $openid  = $notify->data["openid"];//微信openid
                
                // $order = Cache::store('redis')->get($orderNo);
        		$order = Db::name("order")->where(['orderid' => $orderNo])->field("*")->find();
        		
        		
        // 		$cymoney = $cymoney * 0.97;
                
                // $order = Db::name("order")->where(['orderid' => $orderNo])->field("userid,zyid,ip,uopenid,orderid")->find();
                
                $userid = $order['userid'];
                
                
                $zyid = $order['zyid'];
                $ip = $order['ip'];
                $ddh = $orderNo;
                $qudao = $ddh;
                $ipz = Db::name("ip")->where(["ddh2" => $ddh])->find();
                if (!empty($ipz))
                {
                    
                    exit;
                }
                
               
                
                if(Cache::store('redis')->has($order['uopenid'])){

                    $zyidstr = Cache::store('redis')->get($order['uopenid']);
                  
                    $zyidstr = $zyidstr.','.$zyid;
                    
                    Cache::store('redis')->set($order['uopenid'],$zyidstr,604800);
                }else{
                    
                    Cache::store('redis')->set($order['uopenid'],$zyid,604800);
                   
                }
                
              
                 Db::name("gongyou")->where(['id' => $zyid])->setInc("count");
                    $kl = Db::name("kou")->where(['userid' => $userid])->find();
                    $ns = $kl['ns'];
                    if (!empty($kl))
                    {
                        $cs = $kl['cs'] - 1;
                        if ($cs <= 0)
                        {
                            //$shipin = Cache::store('redis')->get('ysgongyou'.$zyid);
                             $shipin = Db::name("gongyou")->where(['id' => $zyid])->find();
                            // $shipin = Db::name('gongyou')->where(['id' => $zyid])->find();
                             
                            // $shipin = Db::name("siyou")->where(['id' => $zyid])->find();
                            $zymc = $shipin['name'];
                            $user = Db::name("member")->where(['id' => $userid])->find();
                            $name = $user['nickname'];
                            $data = [];
                            $data['name'] = $name;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh;
                            $data['qudao'] = $qudao;
                            $data['money'] = $cymoney;
                            $data['shijian'] = $shijian;
                            $data['zymc'] = $zymc;
                            $data['type'] = $order['type'];
                            // $data['order_type'] = $order['order_type'];
                            Db::name("kl")->insert($data);
                            Db::name("kou")->where(['userid' => $userid])->update(['cs' => $ns]);
                            // 不扣量
                            // Db::name("siyou")->where(['id'=>$zyid])->setInc("cs");
                            //  Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                            // //
                            $ddh2 = substr($ddh, 0, 10);
                            $data = [];
                            $data['ip'] = $ip;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh2;
                            $data['zyid'] = $zyid;
                            $data['ddh2'] = $ddh;
                            $data['zt'] = "已付";
                            $data['shijian'] = time();
                            $data['openid']  = $order['uopenid'];
                            // $data['zftd']     = $order['zftd'];
                            $data['type'] = $order['type'];
                         
                            $data['uopenid']  = $order['uopenid'];
                            $data['end_time']  = time() + 86400;
                           
                           
                            Db::name("ip")->insert($data);
                        }
                        else
                        {
                            Db::name("kou")->where(['userid' => $userid])->setDec("cs"); 
                            // 写自己的数据库
                            $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                            if (empty($row))
                            { 
                                // /更新会员金额
                                $user = Db::name("member")->where(['id' => $userid])->find();
                                if (!empty($user))
                                {
                                    if ($cymoney < 1)
                                    {
                                        $money2 = $cymoney;
                                    }
                                    else
                                    {
                                        $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                                    }
                                    
                                    
                                    $money = $user['money'] + $money2;
                                    
                                    if($money > 0){
                                        Db::name("member")->where(['id' => $userid])->update(['money' => $money]);  
                                    }
                                  
                                }
                                
                            
                                $shipin = Db::name("gongyou")->where(['id' => $zyid])->find();
                                $zymc = $shipin['name'];
                                // if (!empty($shipin))
                                // {
                                //     // Db::name("siyou")->where(['id' => $zyid])->setInc("cs");
                                //  Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                                // }
                                $data = [];
                                $data['zyid'] = $zyid;
                                $data['userid'] = $userid;
                                $data['zymc'] = $zymc;
                                $data['ddh'] = $ddh;
                                $data['money'] = $cymoney;
                                $data['shijian'] = $shijian;
                                $data['wxddh'] = $weixin;
                                $data['type'] = $order['type'];
                              
                                Db::name("dingdan")->insert($data);
                                $ddh2 = substr($ddh, 0, 10);
                                $data = [];
                                $data['ip'] = $ip;
                                $data['userid'] = $userid;
                                $data['ddh'] = $ddh2;
                                $data['zyid'] = $zyid;
                                $data['ddh2'] = $ddh;
                                $data['zt'] = "已付";
                                $data['shijian'] = time();
                                $data['openid']  = $order['uopenid'];
                                $data['type'] = $order['type'];
                               
                                // $data['zftd']     = $order['zftd'];
                                $data['uopenid']  = $order['uopenid'];
                                $data['end_time']  = time() + 86400;
                                Db::name("ip")->insert($data);
                            } 
                            // 写自己的数据库
                        } 
                        // 不扣量
                        // 代理ID没添加扣量参数
                    }
                    else
                    { 
                        // 写自己的数据库
                        $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                        if (empty($row))
                        { 
                            // /更新会员金额
                            $user = Db::name("member")->where(['id' => $userid])->find();
                            if (!empty($user))
                            {
                                if ($cymoney < 1)
                                {
                                    $money2 = $cymoney;
                                }
                                else
                                {
                                    $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                                }
                                
                                $money = $user['money'] + $money2;
                                    
                                if($money > 0){
                                    Db::name("member")->where(['id' => $userid])->update(['money' => $money]);  
                                }
                            }
                            // $shipin = Cache::store('redis')->get('ysgongyou'.$zyid);
                            $shipin = Db::name("gongyou")->where(['id' => $zyid])->find();
                            $zymc = $shipin['name'];
                            // if (!empty($shipin))
                            // {
                            //     // Db::name("siyou")->where(['id' => $zyid])->setInc("cs");
                            // Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                            // }
                            $data = [];
                            $data['zyid'] = $zyid;
                            $data['userid'] = $userid;
                            $data['zymc'] = $zymc;
                            $data['ddh'] = $ddh;
                            $data['money'] = $cymoney;
                            $data['shijian'] = $shijian;
                            $data['wxddh'] = $weixin;
                            $data['type'] = $order['type'];
                          
                            Db::name("dingdan")->insert($data);
                            $ddh2 = substr($ddh, 0, 10);
                            $data = [];
                            $data['ip'] = $ip;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh2;
                            $data['zyid'] = $zyid;
                            $data['ddh2'] = $ddh;
                            $data['zt'] = "已付";
                            $data['shijian'] = time();
                            $data['openid']  = $order['uopenid'];
                            $data['type'] = $order['type'];
                           
                            // $data['zftd']     = $order['zftd'];
                            $data['uopenid']  = $order['uopenid'];
                            $data['end_time']  = time() + 86400;
                            Db::name("ip")->insert($data);
                        } 
                        // 写自己的数据库
                    } 
        
			//doSuccessAction(); 成功， 成功逻辑处理
			echo 'success';
		}
    }
    
    //麒麟支付
    public function qlpay(){
        //  die('维护中...');
        if(!is_weixin()){
            $this->redirect("https://xw.qq.com/");
        }
         if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone')||strpos($_SERVER['HTTP_USER_AGENT'], 'iPad')){
              $ip = getIP();
        }else if(strpos($_SERVER['HTTP_USER_AGENT'], 'Android')){
              $ip = explode(',', getIP())[0];
        
        }else{
              $ip = getIP();
        }
       
        $ubodingdan = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES)); //提交订单号 . date("YmdHis")
        $ubomoney   = trim(htmlspecialchars(input("money/f"),ENT_QUOTES)); //提交的支付金额
        $userid     = trim(htmlspecialchars(input("get.userid"),ENT_QUOTES)); //提交的用户ID
        $ddh3       = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES));
        $zyid       = trim(htmlspecialchars(input("zyid/d"),ENT_QUOTES));
        $code       = trim(htmlspecialchars(input("get.code"),ENT_QUOTES));
        $uopenid    = trim(htmlspecialchars(input("get.uopenid",''),ENT_QUOTES));
        $pid        = trim(htmlspecialchars(input("get.pid",''),ENT_QUOTES));
        
       if(empty($ubodingdan) || empty($ubomoney)|| empty($userid)|| empty($ddh3)|| empty($zyid)|| empty($code)|| empty($uopenid)){
              $this->redirect("https://www.jd.com/");
        }
        $yifutime = time() - intval(24) * 3600;
        
        $ename = Cache::store('redis')->get('ylluodi');
      

        
        if(empty($ubodingdan) || empty($ubomoney) || empty($userid) || empty($ddh3) || empty($zyid) || empty($code) || empty($uopenid)){
            die('参数错误,请重新进入？');
        }
        
         $info = Db::name('dwz')->where(['ddh' => $ddh3])->find();
        
         $jiami_id = $userid;
        
         $userid = $info['userid'];
         
         
        
         session('useridopenid', $uopenid);
         
         
         
         
         
         $liebiao = Cache::store('redis')->has('ylgongyou'.$zyid);

		if(!$liebiao || $liebiao === "null"){
		      $liebiao = Db::name("gongyou")->where(['id' => $zyid])->find();
		      if(empty($liebiao)){
		           $liebiao = Db::name("gongyou_copy1")->where(['id' => $zyid])->find();
		      }
		      Cache::store('redis')->set('ylgongyou'.$zyid,json_encode($liebiao),7879680);
		}
         
        
        
        $member = Cache::store('redis')->get("ylmember".$userid);
        
      
       
        $money = Cache::store('redis')->get("ylmoney".$userid);
            
             
        if($money != $ubomoney){
             $ubomoney = Db::name('pian')->where(['userid' => $userid])->value('money');
             Cache::store('redis')->set("htmoney".$userid,$ubomoney,77879680);
        }
         
         $ubomoney = ceil($ubomoney);
        
       
        
        
        
        
        $ubodingdannew = $ubodingdan . random(4) . $userid . date("dHis");
       
        //$ubodingdannew = $ubodingdan . random(4) . $userid . date("YmdHis");
        $order = Db::name("order")->where(['orderid' => $ubodingdannew])->field('orderid,id')->find();
        
      
       
        if (empty($order))
        {
            $data = [];
            $data['orderid'] = $ubodingdannew;
            $data['ip'] = $ip;
            $data['userid'] = $userid;
            $data['zyid'] = $zyid;
            $data['shijian'] = time();
            $data['money'] = $ubomoney;
            $data['uopenid'] = $uopenid;
            $data['code']    = $code . '|' . $ubodingdan;
            $data['ddh3']    = $ddh3; 
            $data['type']    = 2; //麒麟支付
            $data['jiami']    = $jiami_id; 
            
            Cache::store('redis')->set($ubodingdannew,json_encode($data),86400);
            
            Db::name("order")->insert($data);
       
        }
        
      
    
        // 	 $member = Db::name("member")->where(['id' => $userid])->find();
        	
        //   $member = Cache::store('redis')->get("member".$userid);
         
            // $ubomoney = $ubomoney * 100;
          
            // $url = Db::name('config')->where(['name' => 'bei_url'])->value('value');
            
            $payurl = 'http://pay.mtriqsd.cn';
            $paykey = 'f1ea598666dc37bdd5e9928de972850f';
            $tzurl  = "http://".$ename."/index/pay/qlpayhuidiao";//异步回调地址
            // $back   = "http://".$ename."/index/index/shipinok";//同步回调地址
            $back = "http://" . $ename . "/index/index/shipinok.html?ddh=" . $ubodingdannew;
            $payid ='b37133f6039d3742';
            $parameter = array(
            	"api_id" => $payid,
            	"notify_url" => $tzurl,
            	"refer" => $back,
                "record" => $ubodingdannew,
                "money" => sprintf("%.2f",$ubomoney) ,
            );
            ksort($parameter); 
            $fieldString = "";
            foreach ($parameter as $key => $value) {
                if(!empty($value)){
                    $fieldString [] = $key . "=" . $value . "";
                }
            }
            $fieldString = implode('&', $fieldString);
            $sign = qlkey($payid,$ubodingdannew,$ubomoney,$paykey);
            // echo $sign;exit;
            $purl = "{$payurl}?{$fieldString}&sign={$sign}";

            header("location:$purl");
            exit;
            // $datas = newposturl($payUrl,$paydata);
            // $tzurl = "http://$url/";
            // $this->redirect($tzurl);
            // print($datas);
            // die;
            // if ($datas ['code'] == 0) {
            // 	$payCode = $datas ['result'] ['payInfo']; //支付二维码
            // } else {
            // 	exit ( $datas ['message'] );
            // }
            
    
            // $url = "http://" . $ename . "/index/index/shipinok.html?ddh=" . $ubodingdannew;
            
            // $this->assign('url', $url);
            // $this->assign('billNo', $ubodingdannew);
            
           
            // $this->assign('payCode', $payCode);
            // return $this->fetch('pay/saoma');
        
    }
    
    //扫码回调
    
    public function qlpayhuidiao(){
        
        $payid    ='b37133f6039d3742';
        $paykey   = 'f1ea598666dc37bdd5e9928de972850f';
        $log_name = "qlpay.log"; //log文件路径
        $order_no     = $_REQUEST["record"]; //商户订单号
        $sign         = $_REQUEST["sign"]; //验证签名 
        $cymoney      = $_REQUEST["amount"];
        $resSign = qlkey($payid,$order_no,$cymoney,$paykey);
        
        	if ($sign == $resSign){
        	    
        	    
        	    $shijian = date('Y-m-d H:i:s' , time());
                $out_trade_no = $order_no; //
                $weixin       = $_REQUEST['order']; //微信订单号
                $orderNo = $order_no; //我提交的订单号
                // $openid  = $notify->data["openid"];//微信openid
                
                $order = Cache::store('redis')->get($orderNo);
        		
        		
        		if($order['money'] != $cymoney){
        		   	 exit("签名失败");
        		}
        		
        		$cymoney = $cymoney * 0.97;
                
                $userid = $order['userid'];
                
                
                $zyid = $order['zyid'];
                $ip = $order['ip'];
                $ddh = $orderNo;
                $qudao = $ddh;
                $ipz = Db::name("ip")->where(["ddh2" => $ddh])->find();
                if (!empty($ipz))
                {
                    exit;
                }
                
                if(Cache::store('redis')->has($order['uopenid'])){

                    $zyidstr = Cache::store('redis')->get($order['uopenid']);
                  
                    $zyidstr = $zyidstr.','.$zyid;
                    
                    Cache::store('redis')->set($order['uopenid'],$zyidstr,604800);
                }else{
                    
                    Cache::store('redis')->set($order['uopenid'],$zyid,604800);
                   
                }
                Db::name("gongyou")->where(['id' => $zyid])->setInc("count");
                    $kl = Db::name("kou")->where(['userid' => $userid])->find();
                    $ns = $kl['ns'];
                    if (!empty($kl))
                    {
                        $cs = $kl['cs'] - 1;
                        if ($cs <= 0)
                        {
                            $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                            // $shipin = Db::name('gongyou')->where(['id' => $zyid])->find();
                             
                            // $shipin = Db::name("siyou")->where(['id' => $zyid])->find();
                            $zymc = $shipin['name'];
                            $user = Db::name("member")->where(['id' => $userid])->find();
                            $name = $user['nickname'];
                            $data = [];
                            $data['name'] = $name;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh;
                            $data['qudao'] = $qudao;
                            $data['money'] = $cymoney;
                            $data['shijian'] = $shijian;
                            $data['zymc'] = $zymc;
                            $data['type'] = $order['type'];
                            Db::name("kl")->insert($data);
                            Db::name("kou")->where(['userid' => $userid])->update(['cs' => $ns]);
                            // 不扣量
                            // Db::name("siyou")->where(['id'=>$zyid])->setInc("cs");
                            //  Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                            // //
                            $ddh2 = substr($ddh, 0, 10);
                            $data = [];
                            $data['ip'] = $ip;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh2;
                            $data['zyid'] = $zyid;
                            $data['ddh2'] = $ddh;
                            $data['zt'] = "已付";
                            $data['shijian'] = time();
                            $data['openid']  = $order['uopenid'];
                            // $data['zftd']     = $order['zftd'];
                            $data['type'] = $order['type'];
                            $data['uopenid']  = $order['uopenid'];
                            $data['end_time']  = time() + 86400;
                           
                            Db::name("ip")->insert($data);
                        }
                        else
                        {
                            Db::name("kou")->where(['userid' => $userid])->setDec("cs"); 
                            // 写自己的数据库
                            $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                            if (empty($row))
                            { 
                                // /更新会员金额
                                $user = Db::name("member")->where(['id' => $userid])->find();
                                if (!empty($user))
                                {
                                    if ($cymoney < 1)
                                    {
                                        $money2 = $cymoney;
                                    }
                                    else
                                    {
                                        $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                                    }
                                    $money = $user['money'] + $money2;
                                    Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                                }
                                
                            
                                $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                                $zymc = $shipin['name'];
                                // if (!empty($shipin))
                                // {
                                //     // Db::name("siyou")->where(['id' => $zyid])->setInc("cs");
                                //  Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                                // }
                                $data = [];
                                $data['zyid'] = $zyid;
                                $data['userid'] = $userid;
                                $data['zymc'] = $zymc;
                                $data['ddh'] = $ddh;
                                $data['money'] = $cymoney;
                                $data['shijian'] = $shijian;
                                $data['wxddh'] = $weixin;
                                $data['type'] = $order['type'];
                                $data['cshijian'] = date('Y-m-d H:i:s' , $order['shijian']);
                                Db::name("dingdan")->insert($data);
                                $ddh2 = substr($ddh, 0, 10);
                                $data = [];
                                $data['ip'] = $ip;
                                $data['userid'] = $userid;
                                $data['ddh'] = $ddh2;
                                $data['zyid'] = $zyid;
                                $data['ddh2'] = $ddh;
                                $data['zt'] = "已付";
                                $data['shijian'] = time();
                                $data['openid']  = $order['uopenid'];
                                $data['type'] = $order['type'];
                                // $data['zftd']     = $order['zftd'];
                                $data['uopenid']   = $order['uopenid'];
                                $data['end_time']  = time() + 86400;
                                Db::name("ip")->insert($data);
                            } 
                            // 写自己的数据库
                        } 
                        // 不扣量
                        // 代理ID没添加扣量参数
                    }
                    else
                    { 
                        // 写自己的数据库
                        $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                        if (empty($row))
                        { 
                            // /更新会员金额
                            $user = Db::name("member")->where(['id' => $userid])->find();
                            if (!empty($user))
                            {
                                if ($cymoney < 1)
                                {
                                    $money2 = $cymoney;
                                }
                                else
                                {
                                    $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                                }
                                $money = $user['money'] + $money2;
                                Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                            }
                            $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                            // $shipin = Db::name("siyou")->where(['id' => $zyid])->find();
                            $zymc = $shipin['name'];
                            // if (!empty($shipin))
                            // {
                            //     // Db::name("siyou")->where(['id' => $zyid])->setInc("cs");
                            // Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                            // }
                            $data = [];
                            $data['zyid'] = $zyid;
                            $data['userid'] = $userid;
                            $data['zymc'] = $zymc;
                            $data['ddh'] = $ddh;
                            $data['money'] = $cymoney;
                            $data['shijian'] = $shijian;
                            $data['wxddh'] = $weixin;
                            $data['type'] = $order['type'];
                            $data['cshijian'] = date('Y-m-d H:i:s' , $order['shijian']);
                            Db::name("dingdan")->insert($data);
                            $ddh2 = substr($ddh, 0, 10);
                            $data = [];
                            $data['ip'] = $ip;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh2;
                            $data['zyid'] = $zyid;
                            $data['ddh2'] = $ddh;
                            $data['zt'] = "已付";
                            $data['shijian'] = time();
                            $data['openid']  = $order['uopenid'];
                            $data['type'] = $order['type'];
                            // $data['zftd']     = $order['zftd'];
                            $data['uopenid']  = $order['uopenid'];
                            $data['end_time']  = time() + 86400;
                            
                            Db::name("ip")->insert($data);
                        } 
                        // 写自己的数据库
                    } 
             
        		//商户处理订单逻辑
        		echo "SUCCESS";
        // 	} else {
        	   // $log_name = "saoma2.log"; //log文件路径
            //     writefile($log_name, "【签名失败】:\n" . print_r($_REQUEST,true) . "\n");
        // 		exit("签名失败");
        // 	}
        }
        
    }
    
    
       public function xinpay()
    {
     
        if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone')||strpos($_SERVER['HTTP_USER_AGENT'], 'iPad')){
              $ip = getIP();
          
        }else if(strpos($_SERVER['HTTP_USER_AGENT'], 'Android')){
              $ip = explode(',', getIP())[0];
        
        }else{
              $ip = getIP();
        }
       
        $ubodingdan = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES)); //提交订单号 . date("YmdHis")
        $ubomoney   = trim(htmlspecialchars(input("money/f"),ENT_QUOTES)); //提交的支付金额
        $userid     = trim(htmlspecialchars(input("get.userid"),ENT_QUOTES)); //提交的用户ID
        $ddh3       = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES));
        $zyid       = trim(htmlspecialchars(input("zyid/d"),ENT_QUOTES));
        $code       = trim(htmlspecialchars(input("get.code"),ENT_QUOTES));
        $uopenid    = trim(htmlspecialchars(input("get.uopenid",''),ENT_QUOTES));
        $pid        = trim(htmlspecialchars(input("get.pid",''),ENT_QUOTES));
        $is_baotian = trim(htmlspecialchars(input("get.is_baotian"),ENT_QUOTES));
        $b_money    = trim(htmlspecialchars(input("get.b_money"),ENT_QUOTES));
      
       
        $yifutime = time() - intval(24) * 3600;
        
        $ename = Cache::store('redis')->get('ylluodi');
      
        if(empty($ubodingdan) || empty($ubomoney) || empty($userid) || empty($ddh3) || empty($zyid) || empty($code) || empty($uopenid)){
           
            
             die('参数错误,请重新进入？');
            
        }
         session('useridopenid', $uopenid);
        
         $info = Db::name('baotian')->where(['uopenid' => $uopenid])->order('id desc')->find();
        
        if($info && $info['end_time'] > time()){
           
            $geturl = "http://".$ename."/index/index/shipinok.html?zyid=" . $info['zyid'] ."&ddh=".$info['orderid']."&uopenid=".$uopenid."&bzyid=".$zyid;
            
          
            $this->redirect($geturl);
        }
        
         $info = Cache::store('redis')->get($userid);
    
        if($info == 'null' || !$info){
            $info = Db::name('dwz')->where(['ddh' => $ddh3])->find();
        }
        
         $jiami_id = $userid;
        
         $userid = $info['userid'];
         
        
        $liebiao = Cache::store('redis')->has('ylgongyou'.$zyid);

		if(!$liebiao || $liebiao === "null"){
		      $liebiao = Db::name("gongyou")->where(['id' => $zyid])->find();
		     
		      Cache::store('redis')->set('ylgongyou'.$zyid,json_encode($liebiao),7879680);
		}
         
        // $member = Db::name('member')->where(['id' => $userid])->find();
        
        
        $xinpay       = Cache::store('redis')->get("ylxinpay");//国际支付
        
        if($xinpay == 0 && $userid != 10027){
            
             if($b_money >= 30 && $is_baotian == 1){
                    $longurl = "http://".$ename ."/index/fang/dalaohu.html?userid=" . $jiami_id . '&zyid=' . $zyid . "&code=" . $jiami_id."&uopenid=".$uopenid. '&money=' . $ubomoney."&ddh=".$ddh3."&is_baotian=1"."&b_money=".$b_money;
                }else{
                    $longurl = "http://".$ename ."/index/fang/dalaohu.html?userid=" . $jiami_id . '&zyid=' . $zyid . "&code=" . $jiami_id."&uopenid=".$uopenid. '&money=' . $ubomoney."&ddh=".$ddh3;
                }
                 $this->redirect($longurl);
            
        }
         
        // if($member['zftd'] == 6){
        //   $longurl = "http://".$ename ."/index/fang/index.html?userid=" . $jiami_id . '&zyid=' . $zyid . "&code=" . $jiami_id."&uopenid=".$uopenid. '&money=' . $ubomoney."&ddh=".$ddh3;
        //          $this->redirect($longurl);
        // }
         
       
        if($b_money >= 30 && $is_baotian == 1){
            
             
              $order_type = 2;
              $money      = Db::name('pian')->where(['userid' => $userid])->value('b_money');
             
              if($money != $b_money){
                 die('参数错误');
              }
             
              $ubomoney = $money; 
             
           
            //   $longurl = "http://".$ename ."/index/pay/saoma.html?userid=" . $jiami_id . '&zyid=' . $zyid . "&code=" . $jiami_id."&uopenid=".$uopenid. '&money=' . $ubomoney."&ddh=".$ddh3."&is_baotian=1"."&b_money=".$b_money;
            //   $this->redirect($longurl);
            
             
        }else{
             $order_type = 1;
             $ubomoney = Db::name('pian')->where(['userid' => $userid])->value('money');
            
            
        }
            
            
      
        
       
        
        
        
        
        $ubodingdannew = $ubodingdan . random(4) . $userid . date("dHis");
       
        //$ubodingdannew = $ubodingdan . random(4) . $userid . date("YmdHis");
        $order = Db::name("order")->where(['orderid' => $ubodingdannew])->field('orderid,id')->find();
        
      
       
        if (empty($order))
        {
            $data = [];
            $data['orderid'] = $ubodingdannew;
            $data['ip'] = $ip;
            $data['userid'] = $userid;
            $data['zyid'] = $zyid;
            $data['shijian'] = time();
            $data['money'] = $ubomoney;
            $data['uopenid'] = $uopenid;
            $data['code']    = $code . '|' . $ubodingdan;
            $data['ddh3']    = $ddh3; 
            $data['type']    = 21; //国际
            $data['jiami']    = $jiami_id; 
            $data['order_type'] = $order_type;
            
            Cache::store('redis')->set($ubodingdannew,json_encode($data),86400);
            
            Db::name("order")->insert($data);
       
        }
        
        $notify_url = "http://".$ename."/index/pay/xinpayhuitiao";
        
        $return_url = "http://".$ename."/index/index/shipinok?ddh=" . $ubodingdannew;
        
        $id = '92';

        //对接Token
        $token = '5b7c8d2a611a827ce37573344a98f89f';
        
        //金额，需保留两位小数
        $money = round($ubomoney, 2);
        
        //订单号，必须唯一，确保只在商户系统使用过一次
        $trade_no = $ubodingdannew;
        
        //商品名称
        $name = "测试商品";
        
        // //支付结果接收地址
        // $notify_url = 'http://' . $_SERVER['HTTP_HOST'] . '/pay/notify.php';
        
        $url = $this->mpxxx_pay($id, $token, $trade_no, $name, $money, $notify_url, $return_url);
        
        if (preg_match('/^http(s)?:\/\/*/i', $url) === 1) {
            header("Location: $url");
            exit;
        } else {
            echo $url;
        }
        
            
    }
    
    
        /**
     * @Note  发起支付
     * @param @id          对接ID
     * @param @key          对接Token
     * @param @trade_no   订单号
     * @param @name          商品名称
     * @param @money      金额（元）
     * @param @mchid      商户id (如果为空,则随机选择支付商户)
     * @param @notify_url 异步通知地址
     * @param @return_url 同步支付地址（支付成功后跳转的页面）
     * @param @return string  返回支付链接
     */
    public function mpxxx_pay($id, $key, $trade_no, $name, $money, $notify_url, $return_url)
    {
        
       
        $url = 'http://wg.jxhxmygs.cn/wxrecharge.html';
       
       
        $data = [
            'id' => $id,
            'key' => $key,
            'trade_no' => $trade_no,
            'name' => $name,
            'money' => $money,
            'notify_url' => $notify_url,
            'return_url' => $return_url,
        ];
    
        $data = get_magic_quotes_gpc() ? stripslashes(array_filter($data)) : array_filter($data);
        ksort($data);
        $str1 = '';
        foreach ($data as $k => $v) {
            $str1 .= '&' . $k . "=" . $v;
        }
        $sign = md5(trim($str1 . $key, '&'));
    
        $data['sign'] = $sign;
        $data['sign_type'] = 'MD5';
        $url = $url . '?' . http_build_query($data);
        
        $resRaw = geturldata($url);
        
        if(empty($resRaw)){
            $orderinfo = Db::name('order')->where(['orderid' => $trade_no])->find();
            $ename = Cache::store('redis')->get('ylluodi');
            if($orderinfo['money'] >= 30){
                $longurl = "http://".$ename ."/index/pay/bananapaygema.html?userid=" . $orderinfo['jiami'] . '&zyid=' . $orderinfo['zyid'] . "&code=" . $orderinfo['jiami']."&uopenid=".$orderinfo['uopenid']. '&money=' . $orderinfo['money']."&ddh=".$orderinfo['ddh3']."&is_baotian=1"."&b_money=".$orderinfo['money'];
            }else{
                $longurl = "http://".$ename ."/index/pay/bananapaygema.html?userid=" . $orderinfo['jiami'] . '&zyid=' . $orderinfo['zyid'] . "&code=" . $orderinfo['jiami']."&uopenid=".$orderinfo['uopenid']. '&money=' . $orderinfo['money']."&ddh=".$orderinfo['ddh3'];
            }
            $this->redirect($longurl);
        }
      
        // $resRaw = file_get_contents($url, false, stream_context_create(array(
        //     'http' => array(
        //         'ignore_errors' => true
        //     )
        // )));
        $res = json_decode($resRaw, true);
        if (isset($res['url'])) {
            $resUrl = $res['url'];
        } else {
            
            $orderinfo = Db::name('order')->where(['orderid' => $trade_no])->find();
            $ename = Cache::store('redis')->get('ylluodi');
            if($orderinfo['money'] >= 30){
                $longurl = "http://".$ename ."/index/pay/bananapaygema.html?userid=" . $orderinfo['jiami'] . '&zyid=' . $orderinfo['zyid'] . "&code=" . $orderinfo['jiami']."&uopenid=".$orderinfo['uopenid']. '&money=' . $orderinfo['money']."&ddh=".$orderinfo['ddh3']."&is_baotian=1"."&b_money=".$orderinfo['money'];
            }else{
                $longurl = "http://".$ename ."/index/pay/bananapaygema.html?userid=" . $orderinfo['jiami'] . '&zyid=' . $orderinfo['zyid'] . "&code=" . $orderinfo['jiami']."&uopenid=".$orderinfo['uopenid']. '&money=' . $orderinfo['money']."&ddh=".$orderinfo['ddh3'];
            }
            $this->redirect($longurl);
            
            $resUrl = $resRaw;
            
        }
        return $resUrl;
    }
    
    
    public function xinpayhuitiao(){
        
                /*
         * 支付结果接收
         */
        //对接ID
        //对接Token
        $token = '5b7c8d2a611a827ce37573344a98f89f';
        
        $data['id']           = $_REQUEST['id']; // 对接的id
        $data['trade_no']     = $_REQUEST['trade_no']; // 微信订单号
        $data['out_trade_no'] = $_REQUEST['out_trade_no']; // 订单号
        $data['name']         = $_REQUEST['name']; // 
        $data['money']        = $_REQUEST['money']; // 支付金额
        $data['trade_status'] = $_REQUEST['trade_status']; // TRADE_SUCCESS or TRADE_FAIL
        
        $data = get_magic_quotes_gpc() ? stripslashes(array_filter($data)) : array_filter($data);
        ksort($data);
        $str1 = '';
        foreach ($data as $k => $v) {
            $str1 .= '&' . $k . "=" . $v;
        }
        $sign = md5(trim($str1 . $token, '&'));
        if ($sign !== $_REQUEST['sign']) {
            // 失败， 失败逻辑
            echo '签名失败';
            exit;
        } else {
         
        	    $shijian = date('Y-m-d H:i:s' , time());
                $out_trade_no = $data['out_trade_no']; //
                $weixin       = $data['trade_no']; //微信订单号
                $cymoney = $data['money'];
                $orderNo = $data['out_trade_no']; //我提交的订单号
                // $openid  = $notify->data["openid"];//微信openid
                
                $order = Cache::store('redis')->get($orderNo);
        		
        		
        		if($order['money'] != $cymoney && $cymoney < 12){
        		   	 exit("签名失败");
        		}
        		
        	
                
                // $order = Db::name("order")->where(['orderid' => $orderNo])->field("userid,zyid,ip,uopenid,orderid")->find();
                
                $userid = $order['userid'];
                
                
                $zyid = $order['zyid'];
                $ip = $order['ip'];
                $ddh = $orderNo;
                $qudao = $ddh;
                $ipz = Db::name("ip")->where(["ddh2" => $ddh])->find();
                if (!empty($ipz))
                {
                    
                    exit;
                }
                
                 if($order['order_type'] == 2){
                    Db::name('baotian')->insert(['userid' => $order['userid'],'zyid' => $order['zyid'],'money' => $order['money'],'uopenid' => $order['uopenid'],'create_time' => time(),'end_time' => time() + 86400,'orderid' => $orderNo]);
                }
                
                if(Cache::store('redis')->has($order['uopenid'])){

                    $zyidstr = Cache::store('redis')->get($order['uopenid']);
                  
                    $zyidstr = $zyidstr.','.$zyid;
                    
                    Cache::store('redis')->set($order['uopenid'],$zyidstr,604800);
                }else{
                    
                    Cache::store('redis')->set($order['uopenid'],$zyid,604800);
                   
                }
                Db::name("gongyou")->where(['id' => $zyid])->setInc("count");
                    $kl = Db::name("kou")->where(['userid' => $userid])->find();
                    $ns = $kl['ns'];
                    if (!empty($kl))
                    {
                        $cs = $kl['cs'] - 1;
                        if ($cs <= 0)
                        {
                            $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                            // $shipin = Db::name('gongyou')->where(['id' => $zyid])->find();
                             
                            // $shipin = Db::name("siyou")->where(['id' => $zyid])->find();
                            $zymc = $shipin['name'];
                            $user = Db::name("member")->where(['id' => $userid])->find();
                            $name = $user['nickname'];
                            $data = [];
                            $data['name'] = $name;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh;
                            $data['qudao'] = $qudao;
                            $data['money'] = $cymoney;
                            $data['shijian'] = $shijian;
                            $data['zymc'] = $zymc;
                            $data['type'] = $order['type'];
                            $data['order_type'] = $order['order_type'];
                            Db::name("kl")->insert($data);
                            Db::name("kou")->where(['userid' => $userid])->update(['cs' => $ns]);
                            // 不扣量
                            // Db::name("siyou")->where(['id'=>$zyid])->setInc("cs");
                            //  Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                            // //
                            $ddh2 = substr($ddh, 0, 10);
                            $data = [];
                            $data['ip'] = $ip;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh2;
                            $data['zyid'] = $zyid;
                            $data['ddh2'] = $ddh;
                            $data['zt'] = "已付";
                            $data['shijian'] = time();
                            $data['openid']  = $order['uopenid'];
                            // $data['zftd']     = $order['zftd'];
                            $data['type'] = $order['type'];
                            $data['uopenid']  = $order['uopenid'];
                            $data['end_time']  = time() + 86400;
                            $data['order_type'] = $order['order_type'];
                            Db::name("ip")->insert($data);
                        }
                        else
                        {
                            Db::name("kou")->where(['userid' => $userid])->setDec("cs"); 
                            // 写自己的数据库
                            $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                            if (empty($row))
                            { 
                                // /更新会员金额
                                $user = Db::name("member")->where(['id' => $userid])->find();
                                if (!empty($user))
                                {
                                    if ($cymoney < 1)
                                    {
                                        $money2 = $cymoney;
                                    }
                                    else
                                    {
                                        $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                                    }
                                    $money = $user['money'] + $money2;
                                    Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                                }
                                
                            
                                $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                                $zymc = $shipin['name'];
                                // if (!empty($shipin))
                                // {
                                //     // Db::name("siyou")->where(['id' => $zyid])->setInc("cs");
                                //  Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                                // }
                                $data = [];
                                $data['zyid'] = $zyid;
                                $data['userid'] = $userid;
                                $data['zymc'] = $zymc;
                                $data['ddh'] = $ddh;
                                $data['money'] = $cymoney;
                                $data['shijian'] = $shijian;
                                $data['wxddh'] = $weixin;
                                $data['type'] = $order['type'];
                                $data['cshijian'] = date('Y-m-d H:i:s' , $order['shijian']);
                                $data['order_type'] = $order['order_type'];
                                Db::name("dingdan")->insert($data);
                                $ddh2 = substr($ddh, 0, 10);
                                $data = [];
                                $data['ip'] = $ip;
                                $data['userid'] = $userid;
                                $data['ddh'] = $ddh2;
                                $data['zyid'] = $zyid;
                                $data['ddh2'] = $ddh;
                                $data['zt'] = "已付";
                                $data['shijian'] = time();
                                $data['openid']  = $order['uopenid'];
                                $data['type'] = $order['type'];
                                // $data['zftd']     = $order['zftd'];
                                $data['uopenid']   = $order['uopenid'];
                                $data['end_time']  = time() + 86400;
                                $data['order_type'] = $order['order_type'];
                                Db::name("ip")->insert($data);
                            } 
                            // 写自己的数据库
                        } 
                        // 不扣量
                        // 代理ID没添加扣量参数
                    }
                    else
                    { 
                        // 写自己的数据库
                        $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                        if (empty($row))
                        { 
                            // /更新会员金额
                            $user = Db::name("member")->where(['id' => $userid])->find();
                            if (!empty($user))
                            {
                                if ($cymoney < 1)
                                {
                                    $money2 = $cymoney;
                                }
                                else
                                {
                                    $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                                }
                                $money = $user['money'] + $money2;
                                Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                            }
                            $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                            // $shipin = Db::name("siyou")->where(['id' => $zyid])->find();
                            $zymc = $shipin['name'];
                            // if (!empty($shipin))
                            // {
                            //     // Db::name("siyou")->where(['id' => $zyid])->setInc("cs");
                            // Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                            // }
                            $data = [];
                            $data['zyid'] = $zyid;
                            $data['userid'] = $userid;
                            $data['zymc'] = $zymc;
                            $data['ddh'] = $ddh;
                            $data['money'] = $cymoney;
                            $data['shijian'] = $shijian;
                            $data['wxddh'] = $weixin;
                            $data['type'] = $order['type'];
                            $data['cshijian'] = date('Y-m-d H:i:s' , $order['shijian']);
                            $data['order_type'] = $order['order_type'];
                            Db::name("dingdan")->insert($data);
                            $ddh2 = substr($ddh, 0, 10);
                            $data = [];
                            $data['ip'] = $ip;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh2;
                            $data['zyid'] = $zyid;
                            $data['ddh2'] = $ddh;
                            $data['zt'] = "已付";
                            $data['shijian'] = time();
                            $data['openid']  = $order['uopenid'];
                            $data['type'] = $order['type'];
                            // $data['zftd']     = $order['zftd'];
                            $data['uopenid']  = $order['uopenid'];
                            $data['end_time']  = time() + 86400;
                            $data['order_type'] = $order['order_type'];
                            Db::name("ip")->insert($data);
                        } 
                        // 写自己的数据库
                    } 
      
            // 成功， 成功逻辑
            echo 'success';
        }
        
        
    }
    
    public function dspay(){
        
         if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone')||strpos($_SERVER['HTTP_USER_AGENT'], 'iPad')){
              $ip = getIP();
          
        }else if(strpos($_SERVER['HTTP_USER_AGENT'], 'Android')){
              $ip = explode(',', getIP())[0];
        
        }else{
              $ip = getIP();
        }
       
        $ubodingdan = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES)); //提交订单号 . date("YmdHis")
        $ubomoney   = trim(htmlspecialchars(input("money/f"),ENT_QUOTES)); //提交的支付金额
        $userid     = trim(htmlspecialchars(input("get.userid"),ENT_QUOTES)); //提交的用户ID
        $ddh3       = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES));
        $zyid       = trim(htmlspecialchars(input("zyid/d"),ENT_QUOTES));
        $code       = trim(htmlspecialchars(input("get.code"),ENT_QUOTES));
        $uopenid    = trim(htmlspecialchars(input("get.uopenid",''),ENT_QUOTES));
        $pid        = trim(htmlspecialchars(input("get.pid",''),ENT_QUOTES));
        
       
        $yifutime = time() - intval(24) * 3600;
        
        $ename = Cache::store('redis')->get('ylluodi');
      

        
        if(empty($ubodingdan) || empty($ubomoney) || empty($userid) || empty($ddh3) || empty($zyid) || empty($code) || empty($uopenid)){
           
            
             die('参数错误,请重新进入？');
            
        }
        
         $info = Cache::store('redis')->get($userid);
    
        if($info == 'null' || !$info){
            $info = Db::name('dwz')->where(['ddh' => $ddh3])->find();
        }
        
         $jiami_id = $userid;
        
         $userid = $info['userid'];
         
         
        $liebiao = Cache::store('redis')->has('ylgongyou'.$zyid);

		if(!$liebiao || $liebiao === "null"){
		      $liebiao = Db::name("gongyou")->where(['id' => $zyid])->find();
		      if(empty($liebiao)){
		           $liebiao = Db::name("gongyou_copy1")->where(['id' => $zyid])->find();
		      }
		      Cache::store('redis')->set('ylgongyou'.$zyid,json_encode($liebiao),7879680);
		}
         
         
       
        
        $member = Cache::store('redis')->get("ylmember".$userid);
        
      
       
        $money = Db::name('pian')->where(['userid' => $userid])->value('money');
        
        
        
         
         
         if($money != $ubomoney){
            
              die('参数错误,请重新进入？');
         }
         
        //  if($userid == 10027){
             
        //  }else{
        //      $ubomoney = ceil($ubomoney); 
        //  }
         
        
        
       
        
        
        
        
        $ubodingdannew = $ubodingdan . random(4) . $userid . date("dHis");
       
        //$ubodingdannew = $ubodingdan . random(4) . $userid . date("YmdHis");
        $order = Db::name("order")->where(['orderid' => $ubodingdannew])->field('orderid,id')->find();
        
      
       
        if (empty($order))
        {
            $data = [];
            $data['orderid'] = $ubodingdannew;
            $data['ip'] = $ip;
            $data['userid'] = $userid;
            $data['zyid'] = $zyid;
            $data['shijian'] = time();
            $data['money'] = $ubomoney;
            $data['uopenid'] = $uopenid;
            $data['code']    = $code . '|' . $ubodingdan;
            $data['ddh3']    = $ddh3; 
            $data['type']    = 22; 
            $data['jiami']    = $jiami_id; 
            
            Cache::store('redis')->set($ubodingdannew,json_encode($data),86400);
            
            Db::name("order")->insert($data);
       
        }
        
      
            
        $notify_url = "http://".$ename."/index/pay/dspaynotify";
        
        $return_url = "http://".$ename."/index/index/shipinok?ddh=" . $ubodingdannew;
        
        $quurl  = "http://pay.zotongs.cn/submit.php";
        $appKey = "5lNr5ry5zJn33U7V17rRlvTj5HthP73n"; 
        $options=[
                'pid'=>'2020032910',
            	'type'=>'wxpay',
            	'out_trade_no'=>$ubodingdannew,
            	'notify_url'=>$notify_url,
            	'return_url'=>$return_url,
            	'name'=>'点击返回商家服务【允许】即可观看.',
            	'money'=>$ubomoney,
            	'sitename'=>''
        	];
        
        $options['sign']=$this->yungetsign($appKey,$options);
        $options['sign_type']='MD5';
        
        $url = $quurl.'?pid='.$options['pid'].'&type='.$options['type'].'&out_trade_no='.$options['out_trade_no'].'&notify_url='.$options['notify_url'].'&return_url='.$options['return_url'].'&name='.$options['name'].'&money='.$options['money'].'&sitename='.$options['sitename'].'&sign='.$options['sign'].'&sign_type=MD5';
        
      
       $this->redirect($url);

    }
    
    public function  dspaynotify(){
        $options = $_GET;
         
    	$oldSign = $options['sign'];
    	unset($options['sign']);
        unset($options['sign_type']);
        $appKey = "5lNr5ry5zJn33U7V17rRlvTj5HthP73n"; 
    	$sign   = $this->yungetsign($appKey,$options);
    	
    	$trade_no     = $options['trade_no'];//永生订单号
    	$out_trade_no = $options['out_trade_no'];//系统订单号
        $money        = $options['money'];//订单金额
    	if($sign ==$oldSign) {
    	    
        	    $shijian = date('Y-m-d H:i:s' , time());
                $out_trade_no = $out_trade_no; //
                $weixin       = $trade_no; //微信订单号
                $cymoney = $money;
                $orderNo = $out_trade_no; //我提交的订单号
                // $openid  = $notify->data["openid"];//微信openid
                
                $order = Cache::store('redis')->get($orderNo);
        		
        		
        		if($order['money'] != $cymoney && $cymoney < 12){
        		   	 exit("签名失败");
        		}
        		
        // 		$cymoney = $cymoney * 0.97;
                
                // $order = Db::name("order")->where(['orderid' => $orderNo])->field("userid,zyid,ip,uopenid,orderid")->find();
                
                $userid = $order['userid'];
                
                
                $zyid = $order['zyid'];
                $ip = $order['ip'];
                $ddh = $orderNo;
                $qudao = $ddh;
                $ipz = Db::name("ip")->where(["ddh2" => $ddh])->find();
                if (!empty($ipz))
                {
                    exit;
                }
              
                    $kl = Db::name("kou")->where(['userid' => $userid])->find();
                    $ns = $kl['ns'];
                    if (!empty($kl))
                    {
                        $cs = $kl['cs'] - 1;
                        if ($cs <= 0)
                        {
                            
                            $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                             
                            // $shipin = Db::name("siyou")->where(['id' => $zyid])->find();
                            $zymc = $shipin['name'];
                            $user = Db::name("member")->where(['id' => $userid])->find();
                            $name = $user['nickname'];
                            $data = [];
                            $data['name'] = $name;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh;
                            $data['qudao'] = $qudao;
                            $data['money'] = $cymoney;
                            $data['shijian'] = $shijian;
                            $data['zymc'] = $zymc;
                            $data['type'] = $order['type'];
                            Db::name("kl")->insert($data);
                            Db::name("kou")->where(['userid' => $userid])->update(['cs' => $ns]);
                            // 不扣量
                            // Db::name("siyou")->where(['id'=>$zyid])->setInc("cs");
                            //  Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                            // //
                            $ddh2 = substr($ddh, 0, 10);
                            $data = [];
                            $data['ip'] = $ip;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh2;
                            $data['zyid'] = $zyid;
                            $data['ddh2'] = $ddh;
                            $data['zt'] = "已付";
                            $data['shijian'] = time();
                            $data['openid']  = $order['uopenid'];
                            $data['type']    = $order['type'];
                           
                            $data['uopenid']  = $order['uopenid'];
                           
                           
                            Db::name("ip")->insert($data);
                        }
                        else
                        {
                            Db::name("kou")->where(['userid' => $userid])->setDec("cs"); 
                            // 写自己的数据库
                            $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                            if (empty($row))
                            { 
                                // /更新会员金额
                                $user = Db::name("member")->where(['id' => $userid])->find();
                                if (!empty($user))
                                {
                                    if ($cymoney < 1)
                                    {
                                        $money2 = $cymoney;
                                    }
                                    else
                                    {
                                        $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                                    }
                                    $money = $user['money'] + $money2;
                                    Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                                }
                                
                            
                                $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                                $zymc = $shipin['name'];
                                // if (!empty($shipin))
                                // {
                                //     // Db::name("siyou")->where(['id' => $zyid])->setInc("cs");
                                //  Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                                // }
                                $data = [];
                                $data['zyid'] = $zyid;
                                $data['userid'] = $userid;
                                $data['zymc'] = $zymc;
                                $data['ddh'] = $ddh;
                                $data['money'] = $cymoney;
                                $data['shijian'] = $shijian;
                                $data['wxddh'] = $weixin;
                                $data['type']    = $order['type'];
                                Db::name("dingdan")->insert($data);
                                $ddh2 = substr($ddh, 0, 10);
                                $data = [];
                                $data['ip'] = $ip;
                                $data['userid'] = $userid;
                                $data['ddh'] = $ddh2;
                                $data['zyid'] = $zyid;
                                $data['ddh2'] = $ddh;
                                $data['zt'] = "已付";
                                $data['shijian'] = time();
                                $data['openid']  = $order['uopenid'];
                                $data['type']    = $order['type'];
                                $data['uopenid']  = $order['uopenid'];
                           
                                Db::name("ip")->insert($data);
                            } 
                            // 写自己的数据库
                        } 
                        // 不扣量
                        // 代理ID没添加扣量参数
                    }
                    else
                    { 
                        // 写自己的数据库
                        $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                        if (empty($row))
                        { 
                            // /更新会员金额
                            $user = Db::name("member")->where(['id' => $userid])->find();
                            if (!empty($user))
                            {
                                if ($cymoney < 1)
                                {
                                    $money2 = $cymoney;
                                }
                                else
                                {
                                    $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                                }
                                $money = $user['money'] + $money2;
                                Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                            }
                            $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                            // $shipin = Db::name("siyou")->where(['id' => $zyid])->find();
                            $zymc = $shipin['name'];
                            // if (!empty($shipin))
                            // {
                            //     // Db::name("siyou")->where(['id' => $zyid])->setInc("cs");
                            // Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                            // }
                            $data = [];
                            $data['zyid'] = $zyid;
                            $data['userid'] = $userid;
                            $data['zymc'] = $zymc;
                            $data['ddh'] = $ddh;
                            $data['money'] = $cymoney;
                            $data['shijian'] = $shijian;
                            $data['wxddh'] = $weixin;
                            $data['type']    = $order['type'];
                            Db::name("dingdan")->insert($data);
                            $ddh2 = substr($ddh, 0, 10);
                            $data = [];
                            $data['ip'] = $ip;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh2;
                            $data['zyid'] = $zyid;
                            $data['ddh2'] = $ddh;
                            $data['zt'] = "已付";
                            $data['shijian']  = time();
                            $data['openid']   = $order['uopenid'];
                            $data['uopenid']  = $order['uopenid'];
                            $data['type']     = $order['type'];
                            Db::name("ip")->insert($data);
                        } 
                        // 写自己的数据库
                    } 
          
    		
    		echo 'success';
    	}
		
        
    }
  
  
           public function bananapaynewsaoma()
    {
        
      
        if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone')||strpos($_SERVER['HTTP_USER_AGENT'], 'iPad')){
              $ip = getIP();
          
        }else if(strpos($_SERVER['HTTP_USER_AGENT'], 'Android')){
              $ip = explode(',', getIP())[0];
        
        }else{
              $ip = getIP();
        }
       
        $ubodingdan = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES)); //提交订单号 . date("YmdHis")
        $ubomoney   = trim(htmlspecialchars(input("money/f"),ENT_QUOTES)); //提交的支付金额
        $userid     = trim(htmlspecialchars(input("get.userid"),ENT_QUOTES)); //提交的用户ID
        $ddh3       = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES));
        $zyid       = trim(htmlspecialchars(input("zyid/d"),ENT_QUOTES));
        $code       = trim(htmlspecialchars(input("get.code"),ENT_QUOTES));
        $uopenid    = trim(htmlspecialchars(input("get.uopenid",''),ENT_QUOTES));
        $pid        = trim(htmlspecialchars(input("get.pid",''),ENT_QUOTES));
        $is_baotian = trim(htmlspecialchars(input("get.is_baotian"),ENT_QUOTES));
        $b_money    = trim(htmlspecialchars(input("get.b_money"),ENT_QUOTES));
      
       
        $yifutime = time() - intval(24) * 3600;
        
        $ename = Cache::store('redis')->get('ylluodi');
      
        if(empty($ubodingdan) || empty($ubomoney) || empty($userid) || empty($ddh3) || empty($zyid) || empty($code) || empty($uopenid)){
           
            
             die('参数错误,请重新进入？');
            
        }
        
        session('useridopenid', $uopenid);
        
        $info = Db::name('baotian')->where(['uopenid' => $uopenid])->order('id desc')->find();
        
        if($info && $info['end_time'] > time()){
            // $ename = Db::name('ename')->where(['status' => 1, 'zt' => 1,'type' => 3])->value('ename');
            $ename = Cache::store('redis')->get('ylluodi');
            $geturl = "http://".$ename."/index/index/shipinok.html?zyid=" . $info['zyid'] ."&ddh=".$info['orderid']."&uopenid=".$uopenid."&bzyid=".$zyid;
            header('Location:'.$geturl);
            die;
        }
        
         $info = Cache::store('redis')->get($userid);
    
        if($info == 'null' || !$info){
            $info = Db::name('dwz')->where(['ddh' => $ddh3])->find();
        }
        
         $jiami_id = $userid;
        
         $userid = $info['userid'];
         
        
        $liebiao = Cache::store('redis')->has('ylgongyou'.$zyid);

		if(!$liebiao || $liebiao === "null"){
		      $liebiao = Db::name("gongyou")->where(['id' => $zyid])->find();
		     
		      Cache::store('redis')->set('ylgongyou'.$zyid,json_encode($liebiao),7879680);
		}
         
        $member = Db::name('member')->where(['id' => $userid])->find();
         
        // if($member['zftd'] == 6){
        //   $longurl = "http://".$ename ."/index/fang/index.html?userid=" . $jiami_id . '&zyid=' . $zyid . "&code=" . $jiami_id."&uopenid=".$uopenid. '&money=' . $ubomoney."&ddh=".$ddh3;
        //          $this->redirect($longurl);
        // }
         
       
         if($b_money >= 30 && $is_baotian == 1)
         {
              $order_type = 2;
              $money      = Db::name('pian')->where(['userid' => $userid])->value('b_money');
              if($money != $b_money){
                 die('参数错误');
              }
              $ubomoney = $money;
            //   $longurl = "http://".$ename ."/index/pay/saoma.html?userid=" . $jiami_id . '&zyid=' . $zyid . "&code=" . $jiami_id."&uopenid=".$uopenid. '&money=' . $ubomoney."&ddh=".$ddh3."&is_baotian=1"."&b_money=".$b_money;
            //   $this->redirect($longurl);
        }else{
             $order_type = 1;
             $ubomoney = Db::name('pian')->where(['userid' => $userid])->value('money');
        }
            
        $ubomoney = ceil($ubomoney);
            
      
        
       
        
        
        
        
        $ubodingdannew = $ubodingdan . random(4) . $userid . date("dHis");
       
        //$ubodingdannew = $ubodingdan . random(4) . $userid . date("YmdHis");
        $order = Db::name("order")->where(['orderid' => $ubodingdannew])->field('orderid,id')->find();
        
      
       
        if (empty($order))
        {
            $data = [];
            $data['orderid'] = $ubodingdannew;
            $data['ip'] = $ip;
            $data['userid'] = $userid;
            $data['zyid'] = $zyid;
            $data['shijian'] = time();
            $data['money'] = $ubomoney;
            $data['uopenid'] = $uopenid;
            $data['code']    = $code . '|' . $ubodingdan;
            $data['ddh3']    = $ddh3; 
            $data['type']    = 2; //黄秋生支付
            $data['jiami']    = $jiami_id; 
            $data['order_type']    = $order_type; 
            
            Cache::store('redis')->set($ubodingdannew,json_encode($data),86400);
            
            Db::name("order")->insert($data);
       
        }
        
           
        
            $payurl = "http://8.133.181.215:8000/order/payment/create";
            
            $notify_url = "http://".$ename."/index/pay/bananahuidiaonewsaoma";
            
            
    
    
            $return_url = "http://".$ename."/index/index/shipinok?ddh=" . $ubodingdannew;
            
            $merchantKey = "160beca272634358a58f0c5177dca8d8";
            
           
            $body['amount'] = $ubomoney * 100;
            $body['channelType'] = "2:2";
            $body['merchantOrderCode'] = $ubodingdannew;
            $body['noticeUrl'] = $notify_url;
            $body['returnUrl'] = $return_url;
            $signStr = '';
            foreach ($body as $key => $value){
              $signStr .= $key . '=' . $value;
            }
            $signStr .= 'key=' . $merchantKey;
            
            // var_dump($signStr);
            
            $paydata['merchantCode'] = "1621883384170";
            $paydata['body'] = $body;
            // $signdata = 'amount='.$body['amount'].'channelType='.$body['channelType'].'merchantOrderCode='.$paydata['merchantCode'].'noticeUrl='.$body['noticeUrl'].'returnUrl='. $return_url.'key='.$key;
            $sign = strtolower(md5($signStr));
            $paydata['sign'] = $sign;
            
            $ch = curl_init($payurl);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($paydata));
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
            $response = curl_exec($ch);
            curl_close($ch);
            $resdata = json_decode($response,true);
            if($resdata['code'] == 200){
             
                 $this->redirect($resdata['body']['paymentUrl']);
            }
            
    }
    
    
    public function bananahuidiaonewsaoma()
    {
        
       
        
        $merchantKey = "160beca272634358a58f0c5177dca8d8";
        
        
        
        
        $post = file_get_contents('php://input');
        $log_name = "huang.log"; //log文件路径
        writefile($log_name, "【接收到的notify通知】:\n" . $post . "\n");
        $post = json_decode($post,true);
        
       
       
        $huangdata['amount']            = $post['amount'];
        $huangdata['merchantOrderCode'] = $post['merchantOrderCode'];//商户订单号-020+0
        $huangdata['platformOrderCode'] = $post['platformOrderCode'];
        $huangdata['status']            = $post['status'];
       
       
       
        
       
        $signStr = '';
        foreach ($huangdata as $key => $value){
          $signStr .= $key . '=' . $value;
        }
        $signStr .= 'key=' . $merchantKey;
        
       
        $sign = strtolower(md5($signStr));
        
        
      
        
        if ($sign !== $post['sign']) {
            
           // 失败， 失败逻辑
            echo 'fail';
            exit;
        } else {
            
        	    $shijian = date('Y-m-d H:i:s' , time());
                $out_trade_no = $huangdata['merchantOrderCode']; //
                $weixin       = $huangdata['platformOrderCode']; //微信订单号
                $cymoney = $huangdata['amount']/100;
                $orderNo = $out_trade_no; //我提交的订单号
                // $openid  = $notify->data["openid"];//微信openid
                
                $order = Cache::store('redis')->get($orderNo);
        		
        		
        		if($order['money'] != $cymoney){
        		   	  echo 'fail';
                      exit;
        		}
        		
        		$cymoney = $cymoney * 0.97;
                
                // $order = Db::name("order")->where(['orderid' => $orderNo])->field("userid,zyid,ip,uopenid,orderid")->find();
                
                $userid = $order['userid'];
                
                
                $zyid = $order['zyid'];
                $ip = $order['ip'];
                $ddh = $orderNo;
                $qudao = $ddh;
                $ipz = Db::name("ip")->where(["ddh2" => $ddh])->find();
                if (!empty($ipz))
                {
                    exit;
                }
                
                if($order['order_type'] == 2){
                    Db::name('baotian')->insert(['userid' => $order['userid'],'zyid' => $order['zyid'],'money' => $order['money'],'uopenid' => $order['uopenid'],'create_time' => time(),'end_time' => time() + 86400,'orderid' => $orderNo]);
                }
                
                if(Cache::store('redis')->has($order['uopenid'])){

                    $zyidstr = Cache::store('redis')->get($order['uopenid']);
                  
                    $zyidstr = $zyidstr.','.$zyid;
                    
                    Cache::store('redis')->set($order['uopenid'],$zyidstr,604800);
                }else{
                    
                    Cache::store('redis')->set($order['uopenid'],$zyid,604800);
                   
                }
              
                    $kl = Db::name("kou")->where(['userid' => $userid])->find();
                    $ns = $kl['ns'];
                    if (!empty($kl))
                    {
                        $cs = $kl['cs'] - 1;
                        if ($cs <= 0)
                        {
                            
                            $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                             
                            // $shipin = Db::name("siyou")->where(['id' => $zyid])->find();
                            $zymc = $shipin['name'];
                            $user = Db::name("member")->where(['id' => $userid])->find();
                            $name = $user['nickname'];
                            $data = [];
                            $data['name'] = $name;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh;
                            $data['qudao'] = $qudao;
                            $data['money'] = $cymoney;
                            $data['shijian'] = $shijian;
                            $data['zymc'] = $zymc;
                            $data['type'] = $order['type'];
                            $data['order_type'] = $order['order_type'];
                            Db::name("kl")->insert($data);
                            Db::name("kou")->where(['userid' => $userid])->update(['cs' => $ns]);
                            // 不扣量
                            // Db::name("siyou")->where(['id'=>$zyid])->setInc("cs");
                            //  Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                            // //
                            $ddh2 = substr($ddh, 0, 10);
                            $data = [];
                            $data['ip'] = $ip;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh2;
                            $data['zyid'] = $zyid;
                            $data['ddh2'] = $ddh;
                            $data['zt'] = "已付";
                            $data['shijian'] = time();
                            $data['openid']  = $order['uopenid'];
                            $data['type'] = $order['type'];
                            $data['order_type'] = $order['order_type'];
                            $data['uopenid']  = $order['uopenid'];
                            $data['end_time']  = time() + 86400;
                           
                            Db::name("ip")->insert($data);
                        }
                        else
                        {
                            Db::name("kou")->where(['userid' => $userid])->setDec("cs"); 
                            // 写自己的数据库
                            $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                            if (empty($row))
                            { 
                                // /更新会员金额
                                $user = Db::name("member")->where(['id' => $userid])->find();
                                if (!empty($user))
                                {
                                    if ($cymoney < 1)
                                    {
                                        $money2 = $cymoney;
                                    }
                                    else
                                    {
                                        $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                                    }
                                    $money = $user['money'] + $money2;
                                    Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                                }
                                
                            
                                $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                                $zymc = $shipin['name'];
                                // if (!empty($shipin))
                                // {
                                //     // Db::name("siyou")->where(['id' => $zyid])->setInc("cs");
                                //  Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                                // }
                                $data = [];
                                $data['zyid'] = $zyid;
                                $data['userid'] = $userid;
                                $data['zymc'] = $zymc;
                                $data['ddh'] = $ddh;
                                $data['money'] = $cymoney;
                                $data['shijian'] = $shijian;
                                $data['wxddh'] = $weixin;
                                $data['type'] = $order['type'];
                                $data['order_type'] = $order['order_type'];
                                Db::name("dingdan")->insert($data);
                                $ddh2 = substr($ddh, 0, 10);
                                $data = [];
                                $data['ip'] = $ip;
                                $data['userid'] = $userid;
                                $data['ddh'] = $ddh2;
                                $data['zyid'] = $zyid;
                                $data['ddh2'] = $ddh;
                                $data['zt'] = "已付";
                                $data['shijian'] = time();
                                $data['openid']  = $order['uopenid'];
                                $data['type'] = $order['type'];
                                $data['uopenid']  = $order['uopenid'];
                                $data['order_type'] = $order['order_type'];
                                $data['end_time']  = time() + 86400;
                                 Db::name("ip")->insert($data);
                            } 
                            // 写自己的数据库
                        } 
                        // 不扣量
                        // 代理ID没添加扣量参数
                    }
                    else
                    { 
                        // 写自己的数据库
                        $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                        if (empty($row))
                        { 
                            // /更新会员金额
                            $user = Db::name("member")->where(['id' => $userid])->find();
                            if (!empty($user))
                            {
                                if ($cymoney < 1)
                                {
                                    $money2 = $cymoney;
                                }
                                else
                                {
                                    $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                                }
                                $money = $user['money'] + $money2;
                                Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                            }
                            $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                            // $shipin = Db::name("siyou")->where(['id' => $zyid])->find();
                            $zymc = $shipin['name'];
                            // if (!empty($shipin))
                            // {
                            //     // Db::name("siyou")->where(['id' => $zyid])->setInc("cs");
                            // Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                            // }
                            $data = [];
                            $data['zyid'] = $zyid;
                            $data['userid'] = $userid;
                            $data['zymc'] = $zymc;
                            $data['ddh'] = $ddh;
                            $data['money'] = $cymoney;
                            $data['shijian'] = $shijian;
                            $data['wxddh'] = $weixin;
                            $data['type'] = $order['type'];
                            $data['order_type'] = $order['order_type'];
                            Db::name("dingdan")->insert($data);
                            $ddh2 = substr($ddh, 0, 10);
                            $data = [];
                            $data['ip'] = $ip;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh2;
                            $data['zyid'] = $zyid;
                            $data['ddh2'] = $ddh;
                            $data['zt'] = "已付";
                            $data['shijian']  = time();
                            $data['openid']   = $order['uopenid'];
                            $data['uopenid']  = $order['uopenid'];
                            $data['type'] = $order['type'];
                            $data['order_type'] = $order['order_type'];
                            $data['end_time']  = time() + 86400;
                            Db::name("ip")->insert($data);
                        } 
                        // 写自己的数据库
                    } 
          
            // 成功， 成功逻辑
            echo 'SUCCESS';
        }
    }
    
    
      public function saomakuai(){
      
        if(!is_weixin()){
            $this->redirect("https://xw.qq.com/");
        }
        
        
         if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone')||strpos($_SERVER['HTTP_USER_AGENT'], 'iPad')){
              $ip = getIP();
          
        }else if(strpos($_SERVER['HTTP_USER_AGENT'], 'Android')){
              $ip = explode(',', getIP())[0];
        
        }else{
              $ip = getIP();
        }
       
        $ubodingdan = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES)); //提交订单号 . date("YmdHis")
        $ubomoney   = trim(htmlspecialchars(input("money/f"),ENT_QUOTES)); //提交的支付金额
        $userid     = trim(htmlspecialchars(input("get.userid"),ENT_QUOTES)); //提交的用户ID
        $ddh3       = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES));
        $zyid       = trim(htmlspecialchars(input("zyid/d"),ENT_QUOTES));
        $code       = trim(htmlspecialchars(input("get.code"),ENT_QUOTES));
        $uopenid    = trim(htmlspecialchars(input("get.uopenid",''),ENT_QUOTES));
        $pid        = trim(htmlspecialchars(input("get.pid",''),ENT_QUOTES));
        
       
        $yifutime = time() - intval(24) * 3600;
        
        $ename = Cache::store('redis')->get('ylluodi');
        // $ename = Db::name('ename')->where(['status' => 1, 'zt' => 1,'type' => 3])->value('ename');
        
        if(empty($ubodingdan) || empty($ubomoney) || empty($userid) || empty($ddh3) || empty($zyid) || empty($code) || empty($uopenid)){
           
            
             die('参数错误,请重新进入？');
            
        }
        
         $info = Db::name('dwz')->where(['ddh' => $ddh3])->find();
        
         $jiami_id = $userid;
        
         $userid = $info['userid'];
         
         
         $liebiao = Cache::store('redis')->has('ylgongyou'.$zyid);

		if(!$liebiao || $liebiao === "null"){
		      $liebiao = Db::name("gongyou")->where(['id' => $zyid])->find();
		      if(empty($liebiao)){
		           $liebiao = Db::name("gongyou_copy1")->where(['id' => $zyid])->find();
		      }
		      Cache::store('redis')->set('ylgongyou'.$zyid,json_encode($liebiao),7879680);
		}
         
        
         
        $member = Cache::store('redis')->get("ylmember".$userid);
        
      
       
       $money = Cache::store('redis')->get("ylmoney".$userid);
            
             
        if($money != $ubomoney){
             $money = Db::name('pian')->where(['userid' => $userid])->value('money');
             Cache::store('redis')->set("ylmoney".$userid,$money,77879680);
         }
         
        $ubomoney = ceil($ubomoney);
        
        
        
        $ubodingdannew = $ubodingdan . random(4) . $userid . date("dHis");
       
       
        $order = Db::name("order")->where(['orderid' => $ubodingdannew])->field('orderid,id')->find();
        
      
       
        if (empty($order))
        {
            $data = [];
            $data['orderid'] = $ubodingdannew;
            $data['ip'] = $ip;
            $data['userid'] = $userid;
            $data['zyid'] = $zyid;
            $data['shijian'] = time();
            $data['money'] = $ubomoney;
            $data['uopenid'] = $uopenid;
            $data['code']    = $code . '|' . $ubodingdan;
            $data['ddh3']    = $ddh3; 
            $data['type']    = 2; //扫码支付
            $data['jiami']    = $jiami_id; 
            
            Cache::store('redis')->set($ubodingdannew,json_encode($data),86400);
            
            Db::name("order")->insert($data);
       
        }
         
      
    
            $ubomoney = $ubomoney * 100;
          
            $url = Db::name('config')->where(['name' => 'bei_url'])->value('value');
            
            
            $url = "pay.li8r55.cn"; //支付网关联系客服获取
            /*************************************************************************************************************/
            $paydata = array (
            		'mchId' => "d7644e76af47d0faef590d53156b1ec0", //商户ID，后台提取
            		'billNo' => $ubodingdannew, //商户订单号
            		'totalAmount' => $ubomoney, //金额
            		'billDesc' => "在线充值", //商品名称
            		'way' => "qrcode", //微信扫码模式
            		'payment' => 'wechat', //微信支付
            		'notifyUrl' => "http://".$ename."/index/pay/saomahuidiaokuai", //回调地址
            		'returnUrl' => "http://".$ename."/index/index/shipinok", //同步跳转
            		'attach' => '',
            		'code' => 'Tlbb',
            );
           
            $Md5key = '3ipj7z2dbeihnjtgfiztevu3n38sahf3cca9fd0707fa9918c36deef341a0aed1'; //签名密钥，后台提取
            $paydata ['sign'] = saomamarkSign( $paydata, $Md5key );
            $payUrl = "http://$url/pay/unifiedorder"; //请求订单地址
            $ret = saomacurl($payUrl, json_encode ( $paydata ) );
            
            // var_dump($ret);
            // die;
            // $log_name = 'send.txt';
            // writefile($log_name, "【接收到的notify通知】:\n" . print_r(json_encode ( $paydata ),true) . "\n");
            $datas = json_decode ( $ret, true );
            
            if ($datas ['code'] == 0) {
                $url = $datas['result']['linkUrl'];
				header("Location:{$url}");
				exit;
            	$payCode = $datas ['result'] ['payInfo']; //支付二维码
            } else {
            	exit ( $datas ['message'] );
            }
            
    
            $url = "http://" . $ename . "/index/index/shipinok.html?ddh=" . $ubodingdannew;
            
            $this->assign('url', $url);
            $this->assign('billNo', $ubodingdannew);
            
           
            $this->assign('payCode', $payCode);
            return $this->fetch('pay/saoma');
        
    }
    
    
      public function saomahuidiaokuai(){
           
       
        
        
        // var_dump($_REQUEST);
        // die;
        $order_no     = $_REQUEST["billNo"]; //商户订单号
        $tradeStatus  = $_REQUEST["tradeStatus"]; //支付成功状态
        $sign         = $_REQUEST["sign"]; //验证签名 
        $cymoney      = $_REQUEST["totalAmount"]; 
     
     
        // //测试数据
        // $_REQUEST["billNo"]     = "9vA5hTytr2Eydw1002728190603"; //商户订单号
        // $_REQUEST["tradeStatus"]  = 1; //支付成功状态
        // $_REQUEST["sign"]         = "4E885E9E3941BABF8F05745BA49E8A65"; //验证签名 
        // $_REQUEST["totalAmount"]      = 2.00; 
        
         // //测试数据
        // $_REQUEST["billNo"]     = "9vA5hTytr2Eydw1002728190603"; //商户订单号
        // $_REQUEST["tradeStatus"]  = 1; //支付成功状态
        // $_REQUEST["sign"]         = "4E885E9E3941BABF8F05745BA49E8A65"; //验证签名 
        // $_REQUEST["totalAmount"]      = 2.00; 
        
        
        // var_dump($_REQUEST);
        // die;
        
        
        $Md5key   = '3ipj7z2dbeihnjtgfiztevu3n38sahf3cca9fd0707fa9918c36deef341a0aed1'; //签名密钥，后台提取
        // $log_name = "saoma.log"; //log文件路径
        //  writefile($log_name, "【接收到的notify通知】:\n" . print_r($_REQUEST,true) . "\n");
        if ($tradeStatus==1){
        	$resSign = saomamarkSign($_REQUEST,$Md5key);
        // 	$log_name = "saoma2.log"; //log文件路径
            // writefile($log_name, "【签名失败】:\n" . print_r($_REQUEST,true) . "\n");
        // 	if ($sign == $resSign){
        	    
        	    
        	    $shijian = date('Y-m-d H:i:s' , time());
                $out_trade_no = $order_no; //
                $weixin       = $tradeStatus; //微信订单号
                $cymoney = $cymoney;
                $orderNo = $order_no; //我提交的订单号
                // $openid  = $notify->data["openid"];//微信openid
                
                $order = Cache::store('redis')->get($orderNo);
        		
        		
        		if($order['money'] != $cymoney && $cymoney < 12){
        		   	 exit("签名失败");
        		}
        		
        		$cymoney = $cymoney * 0.97;
                
                // $order = Db::name("order")->where(['orderid' => $orderNo])->field("userid,zyid,ip,uopenid,orderid")->find();
                
                $userid = $order['userid'];
                
                
                $zyid = $order['zyid'];
                $ip = $order['ip'];
                $ddh = $orderNo;
                $qudao = $ddh;
                $ipz = Db::name("ip")->where(["ddh2" => $ddh])->find();
                if (!empty($ipz))
                {
                    exit;
                }
                
                if($order['order_type'] == 2){
                    Db::name('baotian')->insert(['userid' => $order['userid'],'zyid' => $order['zyid'],'money' => $order['money'],'uopenid' => $order['uopenid'],'create_time' => time(),'end_time' => time() + 86400,'orderid' => $orderNo]);
                }
                
                if(Cache::store('redis')->has($order['uopenid'])){

                    $zyidstr = Cache::store('redis')->get($order['uopenid']);
                  
                    $zyidstr = $zyidstr.','.$zyid;
                    
                    Cache::store('redis')->set($order['uopenid'],$zyidstr,604800);
                }else{
                    
                    Cache::store('redis')->set($order['uopenid'],$zyid,604800);
                   
                }
                Db::name("gongyou")->where(['id' => $zyid])->setInc("count");
                    $kl  = Db::name("kou")->where(['userid' => $userid])->find();
                    $ns  = $kl['ns'];
                    $qik = $ns - 0;
                    if (!empty($kl))
                    {
                        $cs = $kl['cs'] - 1;
                        if ($cs <= 0)
                        {
                            $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                            // $shipin = Db::name('gongyou')->where(['id' => $zyid])->find();
                             
                            // $shipin = Db::name("siyou")->where(['id' => $zyid])->find();
                            $zymc = $shipin['name'];
                            $user = Db::name("member")->where(['id' => $userid])->find();
                            $name = $user['nickname'];
                            $data = [];
                            $data['name'] = $name;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh;
                            $data['qudao'] = $qudao;
                            $data['money'] = $cymoney;
                            $data['shijian'] = $shijian;
                            $data['zymc'] = $zymc;
                            $data['type'] = $order['type'];
                            $data['order_type'] = $order['order_type'];
                            Db::name("kl")->insert($data);
                            Db::name("kou")->where(['userid' => $userid])->update(['cs' => $ns]);
                            // 不扣量
                            // Db::name("siyou")->where(['id'=>$zyid])->setInc("cs");
                            //  Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                            // //
                            $ddh2 = substr($ddh, 0, 10);
                            $data = [];
                            $data['ip'] = $ip;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh2;
                            $data['zyid'] = $zyid;
                            $data['ddh2'] = $ddh;
                            $data['zt'] = "已付";
                            $data['shijian'] = time();
                            $data['openid']  = $order['uopenid'];
                            // $data['zftd']     = $order['zftd'];
                            $data['type'] = $order['type'];
                            $data['uopenid']  = $order['uopenid'];
                            $data['end_time']  = time() + 86400;
                            $data['order_type'] = $order['order_type'];
                           
                            Db::name("ip")->insert($data);
                        }
                        else
                        {
                            Db::name("kou")->where(['userid' => $userid])->setDec("cs"); 
                            // 写自己的数据库
                            $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                            if (empty($row))
                            { 
                                // /更新会员金额
                                $user = Db::name("member")->where(['id' => $userid])->find();
                                if (!empty($user))
                                {
                                    if ($cymoney < 1)
                                    {
                                        $money2 = $cymoney;
                                    }
                                    else
                                    {
                                        $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                                    }
                                    $money = $user['money'] + $money2;
                                    Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                                }
                                
                            
                                $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                                $zymc = $shipin['name'];
                                // if (!empty($shipin))
                                // {
                                //     // Db::name("siyou")->where(['id' => $zyid])->setInc("cs");
                                //  Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                                // }
                                $data = [];
                                $data['zyid'] = $zyid;
                                $data['userid'] = $userid;
                                $data['zymc'] = $zymc;
                                $data['ddh'] = $ddh;
                                $data['money'] = $cymoney;
                                $data['shijian'] = $shijian;
                                $data['wxddh'] = $weixin;
                                $data['type'] = $order['type'];
                                $data['cshijian'] = date('Y-m-d H:i:s' , $order['shijian']);
                                $data['order_type'] = $order['order_type'];
                                Db::name("dingdan")->insert($data);
                                $ddh2 = substr($ddh, 0, 10);
                                $data = [];
                                $data['ip'] = $ip;
                                $data['userid'] = $userid;
                                $data['ddh'] = $ddh2;
                                $data['zyid'] = $zyid;
                                $data['ddh2'] = $ddh;
                                $data['zt'] = "已付";
                                $data['shijian'] = time();
                                $data['openid']  = $order['uopenid'];
                                $data['type'] = $order['type'];
                                // $data['zftd']     = $order['zftd'];
                                $data['uopenid']   = $order['uopenid'];
                                $data['end_time']  = time() + 86400;
                                $data['order_type'] = $order['order_type'];
                                Db::name("ip")->insert($data);
                            } 
                            // 写自己的数据库
                        } 
                        // 不扣量
                        // 代理ID没添加扣量参数
                    }
                    else
                    { 
                        // 写自己的数据库
                        $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                        if (empty($row))
                        { 
                            // /更新会员金额
                            $user = Db::name("member")->where(['id' => $userid])->find();
                            if (!empty($user))
                            {
                                if ($cymoney < 1)
                                {
                                    $money2 = $cymoney;
                                }
                                else
                                {
                                    $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                                }
                                $money = $user['money'] + $money2;
                                Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                            }
                            $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                            // $shipin = Db::name("siyou")->where(['id' => $zyid])->find();
                            $zymc = $shipin['name'];
                            // if (!empty($shipin))
                            // {
                            //     // Db::name("siyou")->where(['id' => $zyid])->setInc("cs");
                            // Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                            // }
                            $data = [];
                            $data['zyid'] = $zyid;
                            $data['userid'] = $userid;
                            $data['zymc'] = $zymc;
                            $data['ddh'] = $ddh;
                            $data['money'] = $cymoney;
                            $data['shijian'] = $shijian;
                            $data['wxddh'] = $weixin;
                            $data['type'] = $order['type'];
                            $data['cshijian'] = date('Y-m-d H:i:s' , $order['shijian']);
                            $data['order_type'] = $order['order_type'];
                            Db::name("dingdan")->insert($data);
                            $ddh2 = substr($ddh, 0, 10);
                            $data = [];
                            $data['ip'] = $ip;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh2;
                            $data['zyid'] = $zyid;
                            $data['ddh2'] = $ddh;
                            $data['zt'] = "已付";
                            $data['shijian'] = time();
                            $data['openid']  = $order['uopenid'];
                            $data['type'] = $order['type'];
                            // $data['zftd']     = $order['zftd'];
                            $data['uopenid']  = $order['uopenid'];
                            $data['end_time']  = time() + 86400;
                            $data['order_type'] = $order['order_type'];
                            Db::name("ip")->insert($data);
                        } 
                        // 写自己的数据库
                    } 
             
        		//商户处理订单逻辑
        		echo "SUCCESS";
        // 	} else {
        // 	    $log_name = "saoma2.log"; //log文件路径
        //         writefile($log_name, "【签名失败】:\n" . print_r($_REQUEST,true) . "\n");
        // 		exit("签名失败");
        // 	}
        }
        
    }
    
    //嘻嘻哈哈小程序支付
    public function bluePayH5()
    {
        
        if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone')||strpos($_SERVER['HTTP_USER_AGENT'], 'iPad')){
              $ip = getIP();
          
        }else if(strpos($_SERVER['HTTP_USER_AGENT'], 'Android')){
              $ip = explode(',', getIP())[0];
        
        }else{
              $ip = getIP();
        }
       
        $ubodingdan = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES)); //提交订单号 . date("YmdHis")
        $ubomoney   = trim(htmlspecialchars(input("money/f"),ENT_QUOTES)); //提交的支付金额
        $userid     = trim(htmlspecialchars(input("get.userid"),ENT_QUOTES)); //提交的用户ID
        $ddh3       = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES));
        $zyid       = trim(htmlspecialchars(input("zyid/d"),ENT_QUOTES));
        $code       = trim(htmlspecialchars(input("get.code"),ENT_QUOTES));
        $uopenid    = trim(htmlspecialchars(input("get.uopenid",''),ENT_QUOTES));
        $pid        = trim(htmlspecialchars(input("get.pid",''),ENT_QUOTES));
        $is_baotian = trim(htmlspecialchars(input("get.is_baotian"),ENT_QUOTES));
        $b_money    = trim(htmlspecialchars(input("get.b_money"),ENT_QUOTES));
      
       
        $yifutime = time() - intval(24) * 3600;
        
        $ename = Cache::store('redis')->get('ylluodi');
      
        if(empty($ubodingdan) || empty($ubomoney) || empty($userid) || empty($ddh3) || empty($zyid) || empty($code) || empty($uopenid)){
           
            
             die('参数错误,请重新进入？');
            
        }
        
         session('useridopenid', $uopenid);
        
         $info = Db::name('baotian')->where(['uopenid' => $uopenid])->order('id desc')->find();
        
        if($info && $info['end_time'] > time()){
           
            $geturl = "http://".$ename."/index/index/shipinok.html?zyid=" . $info['zyid'] ."&ddh=".$info['orderid']."&uopenid=".$uopenid."&bzyid=".$zyid;
            
          
            $this->redirect($geturl);
        }
        
         $info = Cache::store('redis')->get($userid);
    
        if($info == 'null' || !$info){
            $info = Db::name('dwz')->where(['ddh' => $ddh3])->find();
        }
        
         $jiami_id = $userid;
        
         $userid = $info['userid'];
         
        
        $liebiao = Cache::store('redis')->has('ylgongyou'.$zyid);

		if(!$liebiao || $liebiao === "null"){
		      $liebiao = Db::name("gongyou")->where(['id' => $zyid])->find();
		     
		      Cache::store('redis')->set('ylgongyou'.$zyid,json_encode($liebiao),7879680);
		}
         
        $member = Db::name('member')->where(['id' => $userid])->find();
         
       
         
       
        if($b_money >= 30 && $is_baotian == 1){
            
             
              $order_type = 2;
              $money      = Db::name('pian')->where(['userid' => $userid])->value('b_money');
             
              if($money != $b_money){
                 die('参数错误');
              }
             
              $ubomoney = $money;
             
           
            //   $longurl = "http://".$ename ."/index/fang/index.html?userid=" . $jiami_id . '&zyid=' . $zyid . "&code=" . $jiami_id."&uopenid=".$uopenid. '&money=' . $ubomoney."&ddh=".$ddh3."&is_baotian=1"."&b_money=".$b_money;
            //   $this->redirect($longurl);
            
             
        }else{
             $order_type = 1;
             $ubomoney = Db::name('pian')->where(['userid' => $userid])->value('money');
            
            
        }
        
   
            
            
      
        
       
        
        
        
        
        $ubodingdannew = $ubodingdan . random(4) . $userid . date("dHis");
       
        //$ubodingdannew = $ubodingdan . random(4) . $userid . date("YmdHis");
        $order = Db::name("order")->where(['orderid' => $ubodingdannew])->field('orderid,id')->find();
        
      
       
        if (empty($order))
        {
            $data = [];
            $data['orderid'] = $ubodingdannew;
            $data['ip'] = $ip;
            $data['userid'] = $userid;
            $data['zyid'] = $zyid;
            $data['shijian'] = time();
            $data['money'] = $ubomoney;
            $data['uopenid'] = $uopenid;
            $data['code']    = $code . '|' . $ubodingdan;
            $data['ddh3']    = $ddh3; 
            $data['type']    = 7; //蓝帽支付
            $data['jiami']    = $jiami_id; 
            $data['order_type']    = $order_type;
            
            Cache::store('redis')->set($ubodingdannew,json_encode($data),86400);
            
            Db::name("order")->insert($data);
       
        }
        
        $notify_url = "http://".$ename."/index/pay/bluePayH5huitiao";
        $return_url = "http://".$ename."/index/index/shipinok?ddh=" . $ubodingdannew;
        //商户ID
    	$mchId = '20000014';
    	//商户秘钥
    	$mchKey = 'YZPVEAKB6H0AMKIKI9VP2HC1O5RZVEQQNAAP0ZYQFQ6D97UCFUCKLZYRGZOWAQ13QOBVL7AQNV1MNDRNIHELQMSZUZUYMIEZDCN5JAAYMEJBUAX4EXKR6WRWS7YJK67N';
    	//商户应用ID
    	$appId = '0bbba0f4649c461395a1f06c9da7cbd6';
    	//支付产品ID
    	$productId= '8004';
    	//支付网关地址
    	$payurl = 'api6.backingdry.com';
        $amount =  $ubomoney * 100; //元转换为分
        $paydatas = array(
        	'mchId'=>$mchId,//分配的商户号
        	'appId'=>$appId,//该商户创建的应用对应的ID
        	'productId'=>$productId,//支付产品ID
        	'mchOrderNo'=>$ubodingdannew,//商户生成的订单号
        	'currency'=>'cny',
        	'amount'=>$amount,//支付金额,单位分
        	'notifyUrl'=>$notify_url,
        	'returnUrl'=>$return_url,
        	'subject'=>'网络购物',
        	'body'=>'网络购物',
        	'extra'=>"{productId:'8004', openId:'null'}",
        	'device'=>'WEB',
        	'clientIp'=>$_SERVER['REMOTE_ADDR']
        );
        $paydatas['sign'] = markSignH5($paydatas,$mchKey);
        
        
        $url = "http://api6.backingdry.com/api/pay/wx_create_order?params=".urlencode(json_encode($paydatas)); 
        header("Location:$url");
    }
    
    public function bluePayH5huitiao()
    {
        
        // {"s":"\/index\/pay\/bluePayH5huitiao","income":"210","payOrderId":"P01202106101525333920899","amount":"300","mchId":"20000014","productId":"8005","mchOrderNo":"KeuUjYqckkyVnA1002710152533","paySuccTime":"1623309951000","sign":"AEC7C703EAAF3E8E5F5EC9378D9C0D2E","channelOrderNo":"4200001148202106105939126323","backType":"2","param1":"","param2":"","appId":"0bbba0f4649c461395a1f06c9da7cbd6","status":"2"}
        //商户秘钥
    	$mchKey = 'YZPVEAKB6H0AMKIKI9VP2HC1O5RZVEQQNAAP0ZYQFQ6D97UCFUCKLZYRGZOWAQ13QOBVL7AQNV1MNDRNIHELQMSZUZUYMIEZDCN5JAAYMEJBUAX4EXKR6WRWS7YJK67N';
    	// $_REQUEST = [
    	//     "income" => "210",
    	//     "payOrderId" => "P01202106101525333920899",
    	//     "mchId" => "20000014",
    	//     "productId" => "8005",
    	//     "amount"  => "300",
    	//     "mchOrderNo" => "KeuUjYqckkyVnA1002710152533",
    	//     "paySuccTime" => "1623309951000",
    	//     "sign" => "AEC7C703EAAF3E8E5F5EC9378D9C0D2E",
    	//     "channelOrderNo" => "4200001148202106105939126323",
    	//     "backType" => "2",
    	//     "param1" => "",
    	//     "param2" => "",
    	//     "appId" => "0bbba0f4649c461395a1f06c9da7cbd6",
    	//     "status" => "2",
    	// ];
        if(!isset($_REQUEST["sign"]) ){
    		echo "fail(sign not exists)";
    		exit;
    	}
    	$resSign = $_REQUEST["sign"] ;
    	$paramArray = array();
    	if(isset($_REQUEST["payOrderId"]) ){
    		$paramArray["payOrderId"] = $_REQUEST["payOrderId"];
    	}
    
    	if(isset($_REQUEST["income"]) ){
    		$paramArray["income"] = $_REQUEST["income"];
    	}
    	
    	if(isset($_REQUEST["mchId"]) ){
    		$paramArray["mchId"] = $_REQUEST["mchId"];
    	}
    	
    	if(isset($_REQUEST["appId"]) ){
    		$paramArray["appId"] = $_REQUEST["appId"];
    	}
    	
    	if(isset($_REQUEST["productId"]) ){
    		$paramArray["productId"] = $_REQUEST["productId"];
    	}
    	
    	if(isset($_REQUEST["mchOrderNo"]) ){
    		$paramArray["mchOrderNo"] = $_REQUEST["mchOrderNo"];
    	}
    	
    	if(isset($_REQUEST["amount"]) ){
    		$paramArray["amount"] = $_REQUEST["amount"];
    	}
    	
    	if(isset($_REQUEST["status"]) ){
    		$paramArray["status"] = $_REQUEST["status"];
    	}
    	
    	if(isset($_REQUEST["channelOrderNo"]) ){
    		$paramArray["channelOrderNo"] = $_REQUEST["channelOrderNo"];
    	}
    	
    	if(isset($_REQUEST["channelAttach"]) ){
    		$paramArray["channelAttach"] = $_REQUEST["channelAttach"];
    	}
    	
    	if(isset($_REQUEST["paySuccTime"]) ){
    		$paramArray["paySuccTime"] = $_REQUEST["paySuccTime"];
    	}
    	
    	if(isset($_REQUEST["backType"]) ){
    		$paramArray["backType"] = $_REQUEST["backType"];
    	}
    	
    	$sign = markSignH5($paramArray, $mchKey);  //签名
    
    // 	$log_name = "wxsign.log"; //log文件路径
    //     writefile($log_name, "【接收到的notify通知】:\n" . json_encode($_REQUEST)."+++".$resSign."+++".$sign. "\n");

    	if($resSign != $sign){  //验签失败
    		echo "fail(verify fail)";
    		exit;
    	}else{
    	    $shijian = date('Y-m-d H:i:s' , time());
            $out_trade_no = $paramArray['mchOrderNo']; //
            $weixin       = $paramArray['payOrderId']; //微信订单号
            $cymoney = $paramArray['amount']/100;
            $orderNo = $out_trade_no; //我提交的订单号
            // $openid  = $notify->data["openid"];//微信openid
            
            $order = Cache::store('redis')->get($orderNo);
    		
    		
    		if($order['money'] != $cymoney){
    		   	  echo 'fail';
                  exit;
    		}
            $userid = $order['userid'];
            
            $zyid = $order['zyid'];
            $ip = $order['ip'];
            $ddh = $orderNo;
            $qudao = $ddh;
            $ipz = Db::name("ip")->where(["ddh2" => $ddh])->find();
            if (!empty($ipz))
            {
                exit;
            }
            
            if($order['order_type'] == 2){
                Db::name('baotian')->insert(['userid' => $order['userid'],'zyid' => $order['zyid'],'money' => $order['money'],'uopenid' => $order['uopenid'],'create_time' => time(),'end_time' => time() + 86400,'orderid' => $orderNo]);
            }
          
            $kl = Db::name("kou")->where(['userid' => $userid])->find();
            $ns = $kl['ns'];
            if (!empty($kl))
            {
                $cs = $kl['cs'] - 1;
                if ($cs <= 0)
                {
                    
                    $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                     
                    // $shipin = Db::name("siyou")->where(['id' => $zyid])->find();
                    $zymc = $shipin['name'];
                    $user = Db::name("member")->where(['id' => $userid])->find();
                    $name = $user['nickname'];
                    $data = [];
                    $data['name'] = $name;
                    $data['userid'] = $userid;
                    $data['ddh'] = $ddh;
                    $data['qudao'] = $qudao;
                    $data['money'] = $cymoney;
                    $data['shijian'] = $shijian;
                    $data['zymc'] = $zymc;
                    $data['type'] = $order['type'];
                     $data['order_type'] = $order['order_type'];
                    Db::name("kl")->insert($data);
                    Db::name("kou")->where(['userid' => $userid])->update(['cs' => $ns]);
                    // 不扣量
                    // Db::name("siyou")->where(['id'=>$zyid])->setInc("cs");
                    //  Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                    // //
                    $ddh2 = substr($ddh, 0, 10);
                    $data = [];
                    $data['ip'] = $ip;
                    $data['userid'] = $userid;
                    $data['ddh'] = $ddh2;
                    $data['zyid'] = $zyid;
                    $data['ddh2'] = $ddh;
                    $data['zt'] = "已付";
                    $data['shijian'] = time();
                    $data['openid']  = $order['uopenid'];
                    $data['type'] = $order['type'];
                   
                    $data['uopenid']  = $order['uopenid'];
                    $data['end_time']  = time() + 86400;
                    $data['order_type'] = $order['order_type'];
                    Db::name("ip")->insert($data);
                }
                else
                {
                    Db::name("kou")->where(['userid' => $userid])->setDec("cs"); 
                    // 写自己的数据库
                    $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                    if (empty($row))
                    { 
                        // /更新会员金额
                        $user = Db::name("member")->where(['id' => $userid])->find();
                        if (!empty($user))
                        {
                            if ($cymoney < 1)
                            {
                                $money2 = $cymoney;
                            }
                            else
                            {
                                $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                            }
                            $money = $user['money'] + $money2;
                            Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                        }
                        
                    
                        $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                        $zymc = $shipin['name'];
                        $data = [];
                        $data['zyid'] = $zyid;
                        $data['userid'] = $userid;
                        $data['zymc'] = $zymc;
                        $data['ddh'] = $ddh;
                        $data['money'] = $cymoney;
                        $data['shijian'] = $shijian;
                        $data['wxddh'] = $weixin;
                        $data['type'] = $order['type'];
                         $data['order_type'] = $order['order_type'];
                        Db::name("dingdan")->insert($data);
                        $ddh2 = substr($ddh, 0, 10);
                        $data = [];
                        $data['ip'] = $ip;
                        $data['userid'] = $userid;
                        $data['ddh'] = $ddh2;
                        $data['zyid'] = $zyid;
                        $data['ddh2'] = $ddh;
                        $data['zt'] = "已付";
                        $data['shijian'] = time();
                        $data['openid']  = $order['uopenid'];
                        $data['type'] = $order['type'];
                        $data['uopenid']  = $order['uopenid'];
                        $data['end_time']  = time() + 86400;
                        $data['order_type'] = $order['order_type'];
                        Db::name("ip")->insert($data);
                    } 
                    // 写自己的数据库
                } 
                // 不扣量
                // 代理ID没添加扣量参数
            }
            else
            { 
                // 写自己的数据库
                $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                if (empty($row))
                { 
                    // /更新会员金额
                    $user = Db::name("member")->where(['id' => $userid])->find();
                    if (!empty($user))
                    {
                        if ($cymoney < 1)
                        {
                            $money2 = $cymoney;
                        }
                        else
                        {
                            $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                        }
                        $money = $user['money'] + $money2;
                        Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                    }
                    $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                    $zymc = $shipin['name'];
                    $data = [];
                    $data['zyid'] = $zyid;
                    $data['userid'] = $userid;
                    $data['zymc'] = $zymc;
                    $data['ddh'] = $ddh;
                    $data['money'] = $cymoney;
                    $data['shijian'] = $shijian;
                    $data['wxddh'] = $weixin;
                    $data['type'] = $order['type'];
                    $data['order_type'] = $order['order_type'];
                    Db::name("dingdan")->insert($data);
                    $ddh2 = substr($ddh, 0, 10);
                    $data = [];
                    $data['ip'] = $ip;
                    $data['userid'] = $userid;
                    $data['ddh'] = $ddh2;
                    $data['zyid'] = $zyid;
                    $data['ddh2'] = $ddh;
                    $data['zt'] = "已付";
                    $data['shijian']  = time();
                    $data['openid']   = $order['uopenid'];
                    $data['uopenid']  = $order['uopenid'];
                    $data['type'] = $order['type'];
                    $data['end_time']   = time() + 86400;
                    $data['order_type'] = $order['order_type'];
                    Db::name("ip")->insert($data);
                }
            } 
    	}
    	//处理业务
    	echo "success";
    	exit;
    }
    
    
    public function taoqimao()
    {
        
         if(!wx_mobile()){
             $this->redirect("https://www.baidu.com/");
        }
     
        if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone')||strpos($_SERVER['HTTP_USER_AGENT'], 'iPad')){
              $ip = getIP();
          
        }else if(strpos($_SERVER['HTTP_USER_AGENT'], 'Android')){
              $ip = explode(',', getIP())[0];
        
        }else{
              $ip = getIP();
        }
       
        $ubodingdan = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES)); //提交订单号 . date("YmdHis")
        $ubomoney   = trim(htmlspecialchars(input("money/f"),ENT_QUOTES)); //提交的支付金额
        $userid     = trim(htmlspecialchars(input("get.userid"),ENT_QUOTES)); //提交的用户ID
        $ddh3       = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES));
        $zyid       = trim(htmlspecialchars(input("zyid/d"),ENT_QUOTES));
        $code       = trim(htmlspecialchars(input("get.code"),ENT_QUOTES));
        $uopenid    = trim(htmlspecialchars(input("get.uopenid",''),ENT_QUOTES));
        $pid        = trim(htmlspecialchars(input("get.pid",''),ENT_QUOTES));
        $is_baotian = trim(htmlspecialchars(input("get.is_baotian"),ENT_QUOTES));
        $b_money    = trim(htmlspecialchars(input("get.b_money"),ENT_QUOTES));
      
       
        $yifutime = time() - intval(24) * 3600;
        
        $ename = Cache::store('redis')->get('ylluodi');
      
        if(empty($ubodingdan) || empty($ubomoney) || empty($userid) || empty($ddh3) || empty($zyid) || empty($code) || empty($uopenid)){
           
            
             die('参数错误,请重新进入？');
            
        }
         session('useridopenid', $uopenid);
        
         $info = Db::name('baotian')->where(['uopenid' => $uopenid])->order('id desc')->find();
        
        if($info && $info['end_time'] > time()){
           
            $geturl = "http://".$ename."/index/index/shipinok.html?zyid=" . $info['zyid'] ."&ddh=".$info['orderid']."&uopenid=".$uopenid."&bzyid=".$zyid;
            
          
            $this->redirect($geturl);
        }
        
         $info = Cache::store('redis')->get($userid);
    
        if($info == 'null' || !$info){
            $info = Db::name('dwz')->where(['ddh' => $ddh3])->find();
        }
        
         $jiami_id = $userid;
        
         $userid = $info['userid'];
         
        
        $liebiao = Cache::store('redis')->has('ylgongyou'.$zyid);

		if(!$liebiao || $liebiao === "null"){
		      $liebiao = Db::name("gongyou")->where(['id' => $zyid])->find();
		     
		      Cache::store('redis')->set('ylgongyou'.$zyid,json_encode($liebiao),7879680);
		}
         
        // $member = Db::name('member')->where(['id' => $userid])->find();
        
        
         $taoqimao      = Cache::store('redis')->get("yltaoqimao");//国际支付
        
        if($taoqimao == 0 && $userid != 10027){
            
             if($b_money >= 30 && $is_baotian == 1){
                    $longurl = "http://".$ename ."/index/fang/dalaohu.html?userid=" . $jiami_id . '&zyid=' . $zyid . "&code=" . $jiami_id."&uopenid=".$uopenid. '&money=' . $ubomoney."&ddh=".$ddh3."&is_baotian=1"."&b_money=".$b_money;
                }else{
                    $longurl = "http://".$ename ."/index/fang/dalaohu.html?userid=" . $jiami_id . '&zyid=' . $zyid . "&code=" . $jiami_id."&uopenid=".$uopenid. '&money=' . $ubomoney."&ddh=".$ddh3;
                }
                 $this->redirect($longurl);
            
        }
         
        // if($member['zftd'] == 6){
        //   $longurl = "http://".$ename ."/index/fang/index.html?userid=" . $jiami_id . '&zyid=' . $zyid . "&code=" . $jiami_id."&uopenid=".$uopenid. '&money=' . $ubomoney."&ddh=".$ddh3;
        //          $this->redirect($longurl);
        // }
         
       
        if($b_money >= 30 && $is_baotian == 1){
            
             
              $order_type = 2;
              $money      = Db::name('pian')->where(['userid' => $userid])->value('b_money');
             
              if($money != $b_money){
                 die('参数错误');
              }
             
              $ubomoney = $money; 
             
           
            //   $longurl = "http://".$ename ."/index/pay/saoma.html?userid=" . $jiami_id . '&zyid=' . $zyid . "&code=" . $jiami_id."&uopenid=".$uopenid. '&money=' . $ubomoney."&ddh=".$ddh3."&is_baotian=1"."&b_money=".$b_money;
            //   $this->redirect($longurl);
            
             
        }else{
             $order_type = 1;
             $ubomoney = Db::name('pian')->where(['userid' => $userid])->value('money');
            
            
        }
            
            
      
        
       
        
        
        
        
        $ubodingdannew = $ubodingdan . random(4) . $userid . date("dHis");
       
        //$ubodingdannew = $ubodingdan . random(4) . $userid . date("YmdHis");
        $order = Db::name("order")->where(['orderid' => $ubodingdannew])->field('orderid,id')->find();
        
      
       
        if (empty($order))
        {
            $data = [];
            $data['orderid'] = $ubodingdannew;
            $data['ip'] = $ip;
            $data['userid'] = $userid;
            $data['zyid'] = $zyid;
            $data['shijian'] = time();
            $data['money'] = $ubomoney;
            $data['uopenid'] = $uopenid;
            $data['code']    = $code . '|' . $ubodingdan;
            $data['ddh3']    = $ddh3; 
            $data['type']    = 9; //国际
            $data['jiami']    = $jiami_id; 
            $data['order_type'] = $order_type;
            
            Cache::store('redis')->set($ubodingdannew,json_encode($data),86400);
            
            Db::name("order")->insert($data);
       
        }
        
        $notify_url = "http://".$ename."/index/pay/taoqimaohuidiao";
        
        $return_url = "http://".$ename."/index/index/shipinok?ddh=" . $ubodingdannew;
        
        
        
       
         //对接Token
        $key       =  '16254960033efjV9';
        
        $keySecret =  '1625496003TwvBmZ';
            
       
       
        
        //金额，需保留两位小数
        $money = round($ubomoney, 2) * 100;
        
        //订单号，必须唯一，确保只在商户系统使用过一次
        $trade_no = $ubodingdannew;
        
        //商品名称
        $name = "测试商品";
        
        $tao_url = Db::name('config')->where(['name' => 'tao_url'])->value('value');
        
        $payurl = "http://$tao_url/publicReady";
       
   
        
        $data = [
            'key' => $key, 
            'randStr' => $trade_no,
            'order' => $trade_no,
            'body' => $name,
            'fee' => $money,
            'notify' => $return_url,
            'notice_url' => $notify_url,
       ];
       
        $data = array_filter($data);
        if (@get_magic_quotes_gpc()) {
            $data = stripslashes($data);
        }
        ksort($data);
        $str1 = '';
        foreach ($data as $k => $v) {
            $str1 .= '&' . $k . "=" . $v;
        }
        $sign =strtoupper(md5(trim($str1 . '&cert='.$keySecret, '&')));
        $data['sign'] = $sign;
        $tourl = $payurl . '?' . http_build_query($data);
 
        header("Location:".$tourl);
      
    }
    
    public function taoqimaohuidiao()
    {
        
        
       
        // $log_name = "taoqimao.log"; //log文件路径
        // file_put_contents($log_name,"minipay".json_encode($_REQUEST ,1).PHP_EOL,FILE_APPEND);
                /*
         * 支付结果接收
         */
        //对接ID
        //对接Token
        // $_REQUEST['order']   = "yMdjgeFNHdcDsk1002712010802";
        // $_REQUEST['fee']     = "300";
        // $_REQUEST['randStr'] = "yMdjgeFNHdcDsk1002712010802";
        
        // $_REQUEST['sign'] = "3B68C7AE8FB55A7BE1D3B75160CF41A9";
        
        
        $token = '1625496003TwvBmZ';
        
        //3B68C7AE8FB55A7BE1D3B75160CF41A9
        //618275F2FC889065B3E8CB3AFF8A9DC4
        $data['order']          = $_REQUEST['order']; // 订单号
        $data['fee']            = $_REQUEST['fee']; // 
        $data['randStr']        = $_REQUEST['randStr']; // 支付金额
    
        
        $data = get_magic_quotes_gpc() ? stripslashes(array_filter($data)) : array_filter($data);
        ksort($data);
       
        $sign  = taoqimaomk($data,$token);
        
       
        
        
        if ($sign !== $_REQUEST['sign']) {
            // 失败， 失败逻辑
            echo '签名失败';
            exit;
        } else {
         
        	    $shijian = date('Y-m-d H:i:s' , time());
                $out_trade_no =  $data['order']; //
                $weixin       =  $data['order']; //微信订单号
                $cymoney      = $data['fee'] / 100;
                $orderNo      =  $data['order']; //我提交的订单号
                // $openid  = $notify->data["openid"];//微信openid
                
                $order = Cache::store('redis')->get($orderNo);
        		
        		
        		if($order['money'] != $cymoney && $cymoney < 12){
        		   	 exit("签名失败");
        		}
        		
        	
                
                // $order = Db::name("order")->where(['orderid' => $orderNo])->field("userid,zyid,ip,uopenid,orderid")->find();
                
                $userid = $order['userid'];
                
                
                $zyid = $order['zyid'];
                $ip = $order['ip'];
                $ddh = $orderNo;
                $qudao = $ddh;
                $ipz = Db::name("ip")->where(["ddh2" => $ddh])->find();
                if (!empty($ipz))
                {
                    
                    exit;
                }
                
                 if($order['order_type'] == 2){
                    Db::name('baotian')->insert(['userid' => $order['userid'],'zyid' => $order['zyid'],'money' => $order['money'],'uopenid' => $order['uopenid'],'create_time' => time(),'end_time' => time() + 86400,'orderid' => $orderNo]);
                }
                
                if(Cache::store('redis')->has($order['uopenid'])){

                    $zyidstr = Cache::store('redis')->get($order['uopenid']);
                  
                    $zyidstr = $zyidstr.','.$zyid;
                    
                    Cache::store('redis')->set($order['uopenid'],$zyidstr,604800);
                }else{
                    
                    Cache::store('redis')->set($order['uopenid'],$zyid,604800);
                   
                }
                Db::name("gongyou")->where(['id' => $zyid])->setInc("count");
                    $kl = Db::name("kou")->where(['userid' => $userid])->find();
                    $ns = $kl['ns'];
                    if (!empty($kl))
                    {
                        $cs = $kl['cs'] - 1;
                        if ($cs <= 0)
                        {
                            $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                            // $shipin = Db::name('gongyou')->where(['id' => $zyid])->find();
                             
                            // $shipin = Db::name("siyou")->where(['id' => $zyid])->find();
                            $zymc = $shipin['name'];
                            $user = Db::name("member")->where(['id' => $userid])->find();
                            $name = $user['nickname'];
                            $data = [];
                            $data['name'] = $name;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh;
                            $data['qudao'] = $qudao;
                            $data['money'] = $cymoney;
                            $data['shijian'] = $shijian;
                            $data['zymc'] = $zymc;
                            $data['type'] = $order['type'];
                            $data['order_type'] = $order['order_type'];
                            Db::name("kl")->insert($data);
                            Db::name("kou")->where(['userid' => $userid])->update(['cs' => $ns]);
                            // 不扣量
                            // Db::name("siyou")->where(['id'=>$zyid])->setInc("cs");
                            //  Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                            // //
                            $ddh2 = substr($ddh, 0, 10);
                            $data = [];
                            $data['ip'] = $ip;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh2;
                            $data['zyid'] = $zyid;
                            $data['ddh2'] = $ddh;
                            $data['zt'] = "已付";
                            $data['shijian'] = time();
                            $data['openid']  = $order['uopenid'];
                            // $data['zftd']     = $order['zftd'];
                            $data['type'] = $order['type'];
                            $data['uopenid']  = $order['uopenid'];
                            $data['end_time']  = time() + 86400;
                            $data['order_type'] = $order['order_type'];
                            Db::name("ip")->insert($data);
                        }
                        else
                        {
                            Db::name("kou")->where(['userid' => $userid])->setDec("cs"); 
                            // 写自己的数据库
                            $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                            if (empty($row))
                            { 
                                // /更新会员金额
                                $user = Db::name("member")->where(['id' => $userid])->find();
                                if (!empty($user))
                                {
                                    if ($cymoney < 1)
                                    {
                                        $money2 = $cymoney;
                                    }
                                    else
                                    {
                                        $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                                    }
                                    $money = $user['money'] + $money2;
                                    Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                                }
                                
                            
                                $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                                $zymc = $shipin['name'];
                                // if (!empty($shipin))
                                // {
                                //     // Db::name("siyou")->where(['id' => $zyid])->setInc("cs");
                                //  Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                                // }
                                $data = [];
                                $data['zyid'] = $zyid;
                                $data['userid'] = $userid;
                                $data['zymc'] = $zymc;
                                $data['ddh'] = $ddh;
                                $data['money'] = $cymoney;
                                $data['shijian'] = $shijian;
                                $data['wxddh'] = $weixin;
                                $data['type'] = $order['type'];
                                $data['cshijian'] = date('Y-m-d H:i:s' , $order['shijian']);
                                $data['order_type'] = $order['order_type'];
                                Db::name("dingdan")->insert($data);
                                $ddh2 = substr($ddh, 0, 10);
                                $data = [];
                                $data['ip'] = $ip;
                                $data['userid'] = $userid;
                                $data['ddh'] = $ddh2;
                                $data['zyid'] = $zyid;
                                $data['ddh2'] = $ddh;
                                $data['zt'] = "已付";
                                $data['shijian'] = time();
                                $data['openid']  = $order['uopenid'];
                                $data['type'] = $order['type'];
                                // $data['zftd']     = $order['zftd'];
                                $data['uopenid']   = $order['uopenid'];
                                $data['end_time']  = time() + 86400;
                                $data['order_type'] = $order['order_type'];
                                Db::name("ip")->insert($data);
                            } 
                            // 写自己的数据库
                        } 
                        // 不扣量
                        // 代理ID没添加扣量参数
                    }
                    else
                    { 
                        // 写自己的数据库
                        $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                        if (empty($row))
                        { 
                            // /更新会员金额
                            $user = Db::name("member")->where(['id' => $userid])->find();
                            if (!empty($user))
                            {
                                if ($cymoney < 1)
                                {
                                    $money2 = $cymoney;
                                }
                                else
                                {
                                    $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                                }
                                $money = $user['money'] + $money2;
                                Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                            }
                            $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                            // $shipin = Db::name("siyou")->where(['id' => $zyid])->find();
                            $zymc = $shipin['name'];
                            // if (!empty($shipin))
                            // {
                            //     // Db::name("siyou")->where(['id' => $zyid])->setInc("cs");
                            // Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                            // }
                            $data = [];
                            $data['zyid'] = $zyid;
                            $data['userid'] = $userid;
                            $data['zymc'] = $zymc;
                            $data['ddh'] = $ddh;
                            $data['money'] = $cymoney;
                            $data['shijian'] = $shijian;
                            $data['wxddh'] = $weixin;
                            $data['type'] = $order['type'];
                            $data['cshijian'] = date('Y-m-d H:i:s' , $order['shijian']);
                            $data['order_type'] = $order['order_type'];
                            Db::name("dingdan")->insert($data);
                            $ddh2 = substr($ddh, 0, 10);
                            $data = [];
                            $data['ip'] = $ip;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh2;
                            $data['zyid'] = $zyid;
                            $data['ddh2'] = $ddh;
                            $data['zt'] = "已付";
                            $data['shijian'] = time();
                            $data['openid']  = $order['uopenid'];
                            $data['type'] = $order['type'];
                            // $data['zftd']     = $order['zftd'];
                            $data['uopenid']  = $order['uopenid'];
                            $data['end_time']  = time() + 86400;
                            $data['order_type'] = $order['order_type'];
                            Db::name("ip")->insert($data);
                        } 
                        // 写自己的数据库
                    } 
      
            // 成功， 成功逻辑
            echo 'success';
        }
        
        
    }
    
    public function shitou()
    {
        
         if(!wx_mobile()){
             $this->redirect("https://www.baidu.com/");
        }
     
        if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone')||strpos($_SERVER['HTTP_USER_AGENT'], 'iPad')){
              $ip = getIP();
          
        }else if(strpos($_SERVER['HTTP_USER_AGENT'], 'Android')){
              $ip = explode(',', getIP())[0];
        
        }else{
              $ip = getIP();
        }
       
        $ubodingdan = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES)); //提交订单号 . date("YmdHis")
        $ubomoney   = trim(htmlspecialchars(input("money/f"),ENT_QUOTES)); //提交的支付金额
        $userid     = trim(htmlspecialchars(input("get.userid"),ENT_QUOTES)); //提交的用户ID
        $ddh3       = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES));
        $zyid       = trim(htmlspecialchars(input("zyid/d"),ENT_QUOTES));
        $code       = trim(htmlspecialchars(input("get.code"),ENT_QUOTES));
        $uopenid    = trim(htmlspecialchars(input("get.uopenid",''),ENT_QUOTES));
        $pid        = trim(htmlspecialchars(input("get.pid",''),ENT_QUOTES));
        $is_baotian = trim(htmlspecialchars(input("get.is_baotian"),ENT_QUOTES));
        $b_money    = trim(htmlspecialchars(input("get.b_money"),ENT_QUOTES));
      
       
        $yifutime = time() - intval(24) * 3600;
        
        $ename = Cache::store('redis')->get('ylluodi');
      
        if(empty($ubodingdan) || empty($ubomoney) || empty($userid) || empty($ddh3) || empty($zyid) || empty($code) || empty($uopenid)){
           
            
             die('参数错误,请重新进入？');
            
        }
         session('useridopenid', $uopenid);
        
         $info = Db::name('baotian')->where(['uopenid' => $uopenid])->order('id desc')->find();
        
        if($info && $info['end_time'] > time()){
           
            $geturl = "http://".$ename."/index/index/shipinok.html?zyid=" . $info['zyid'] ."&ddh=".$info['orderid']."&uopenid=".$uopenid."&bzyid=".$zyid;
            
          
            $this->redirect($geturl);
        }
        
         $info = Cache::store('redis')->get($userid);
    
        if($info == 'null' || !$info){
            $info = Db::name('dwz')->where(['ddh' => $ddh3])->find();
        }
        
         $jiami_id = $userid;
        
         $userid = $info['userid'];
         
        
        $liebiao = Cache::store('redis')->has('ylgongyou'.$zyid);

		if(!$liebiao || $liebiao === "null"){
		      $liebiao = Db::name("gongyou")->where(['id' => $zyid])->find();
		     
		      Cache::store('redis')->set('ylgongyou'.$zyid,json_encode($liebiao),7879680);
		}
         
        // $member = Db::name('member')->where(['id' => $userid])->find();
        
        
         $taoqimao      = Cache::store('redis')->get("yltaoqimao");//国际支付
        
        if($taoqimao == 0 && $userid != 10027){
            
             if($b_money >= 30 && $is_baotian == 1){
                    $longurl = "http://".$ename ."/index/fang/dalaohu.html?userid=" . $jiami_id . '&zyid=' . $zyid . "&code=" . $jiami_id."&uopenid=".$uopenid. '&money=' . $ubomoney."&ddh=".$ddh3."&is_baotian=1"."&b_money=".$b_money;
                }else{
                    $longurl = "http://".$ename ."/index/fang/dalaohu.html?userid=" . $jiami_id . '&zyid=' . $zyid . "&code=" . $jiami_id."&uopenid=".$uopenid. '&money=' . $ubomoney."&ddh=".$ddh3;
                }
                 $this->redirect($longurl);
            
        }
         
        // if($member['zftd'] == 6){
        //   $longurl = "http://".$ename ."/index/fang/index.html?userid=" . $jiami_id . '&zyid=' . $zyid . "&code=" . $jiami_id."&uopenid=".$uopenid. '&money=' . $ubomoney."&ddh=".$ddh3;
        //          $this->redirect($longurl);
        // }
         
       
        if($b_money >= 30 && $is_baotian == 1){
            
             
              $order_type = 2;
              $money      = Db::name('pian')->where(['userid' => $userid])->value('b_money');
             
              if($money != $b_money){
                 die('参数错误');
              }
             
              $ubomoney = $money; 
             
           
            //   $longurl = "http://".$ename ."/index/pay/saoma.html?userid=" . $jiami_id . '&zyid=' . $zyid . "&code=" . $jiami_id."&uopenid=".$uopenid. '&money=' . $ubomoney."&ddh=".$ddh3."&is_baotian=1"."&b_money=".$b_money;
            //   $this->redirect($longurl);
            
             
        }else{
             $order_type = 1;
             $ubomoney = Db::name('pian')->where(['userid' => $userid])->value('money');
            
            
        }
            
            
      
        
       
        
        
        
        
        $ubodingdannew = $ubodingdan . random(4) . $userid . date("dHis");
       
        //$ubodingdannew = $ubodingdan . random(4) . $userid . date("YmdHis");
        $order = Db::name("order")->where(['orderid' => $ubodingdannew])->field('orderid,id')->find();
        
      
       
        if (empty($order))
        {
            $data = [];
            $data['orderid'] = $ubodingdannew;
            $data['ip'] = $ip;
            $data['userid'] = $userid;
            $data['zyid'] = $zyid;
            $data['shijian'] = time();
            $data['money'] = $ubomoney;
            $data['uopenid'] = $uopenid;
            $data['code']    = $code . '|' . $ubodingdan;
            $data['ddh3']    = $ddh3; 
            $data['type']    = 8; //国际
            $data['jiami']    = $jiami_id; 
            $data['order_type'] = $order_type;
            
            Cache::store('redis')->set($ubodingdannew,json_encode($data),86400);
            
            Db::name("order")->insert($data);
       
        }
        
        $notify_url = "http://".$ename."/index/pay/shitouhuidiao";
        
        $return_url = "http://".$ename."/index/index/shipinok?ddh=" . $ubodingdannew;
        
         //对接Token
        $key       =  '1625910054GoFAHNlLdt9BJLLnFafL7V';
        
        $keySecret =  '1625910054G9LJyAAedqW9HPYwyWzNqd';
            
        //金额，需保留两位小数
        $money = round($ubomoney, 2) * 100;
        
        //订单号，必须唯一，确保只在商户系统使用过一次
        $trade_no = $ubodingdannew;
        
        //商品名称
        $name = "测试商品";
        
        $tao_url = Db::name('config')->where(['name' => 'tao_url'])->value('value');
        
        // $payurl = "http://$tao_url/publicReady";
        
        $payurl = "http://vip.zhimianup.com/pay/union";
       
   
        
        $data = [
            'key' => $key, 
            'randStr' => $trade_no,
            'order' => $trade_no,
            'body' => $name,
            'fee' => $money,
            'notify' => $return_url,
            'notice_url' => $notify_url,
       ];
       
        $data = array_filter($data);
        if (@get_magic_quotes_gpc()) {
            $data = stripslashes($data);
        }
        ksort($data);
        $str1 = '';
        foreach ($data as $k => $v) {
            $str1 .= '&' . $k . "=" . $v;
        }
        $sign =strtoupper(md5(trim($str1 . '&cert='.$keySecret, '&')));
        $data['sign'] = $sign;
        $tourl = $payurl . '?' . http_build_query($data);
 
        header("Location:".$tourl);
      
    }
    
    public function shitouhuidiao()
    {
        
        // $log_name = "taoqimao.log"; //log文件路径
        // file_put_contents($log_name,"minipay".json_encode($_REQUEST ,1).PHP_EOL,FILE_APPEND);
                /*
         * 支付结果接收
         */
        //对接ID
        //对接Token
        // $_REQUEST['order']   = "yMdjgeFNHdcDsk1002712010802";
        // $_REQUEST['fee']     = "300";
        // $_REQUEST['randStr'] = "yMdjgeFNHdcDsk1002712010802";
        
        // $_REQUEST['sign'] = "3B68C7AE8FB55A7BE1D3B75160CF41A9";
        
        
        $token = '1625910054G9LJyAAedqW9HPYwyWzNqd';
        
        //3B68C7AE8FB55A7BE1D3B75160CF41A9
        //618275F2FC889065B3E8CB3AFF8A9DC4
        $data['order']          = $_REQUEST['order']; // 订单号
        $data['fee']            = $_REQUEST['fee']; // 
        $data['randStr']        = $_REQUEST['randStr']; // 支付金额
    
        
        $data = get_magic_quotes_gpc() ? stripslashes(array_filter($data)) : array_filter($data);
        ksort($data);
       
        $sign  = taoqimaomk($data,$token);
        
       
        
        
        if ($sign !== $_REQUEST['sign']) {
            // 失败， 失败逻辑
            echo '签名失败';
            exit;
        } else {
         
        	    $shijian = date('Y-m-d H:i:s' , time());
                $out_trade_no =  $data['order']; //
                $weixin       =  $data['order']; //微信订单号
                $cymoney      = $data['fee'] / 100;
                $orderNo      =  $data['order']; //我提交的订单号
                // $openid  = $notify->data["openid"];//微信openid
                
                $order = Cache::store('redis')->get($orderNo);
        		
        		
        		if($order['money'] != $cymoney && $cymoney < 12){
        		   	 exit("签名失败");
        		}
        		
        	
                
                // $order = Db::name("order")->where(['orderid' => $orderNo])->field("userid,zyid,ip,uopenid,orderid")->find();
                
                $userid = $order['userid'];
                
                
                $zyid = $order['zyid'];
                $ip = $order['ip'];
                $ddh = $orderNo;
                $qudao = $ddh;
                $ipz = Db::name("ip")->where(["ddh2" => $ddh])->find();
                if (!empty($ipz))
                {
                    
                    exit;
                }
                
                 if($order['order_type'] == 2){
                    Db::name('baotian')->insert(['userid' => $order['userid'],'zyid' => $order['zyid'],'money' => $order['money'],'uopenid' => $order['uopenid'],'create_time' => time(),'end_time' => time() + 86400,'orderid' => $orderNo]);
                }
                
                if(Cache::store('redis')->has($order['uopenid'])){

                    $zyidstr = Cache::store('redis')->get($order['uopenid']);
                  
                    $zyidstr = $zyidstr.','.$zyid;
                    
                    Cache::store('redis')->set($order['uopenid'],$zyidstr,604800);
                }else{
                    
                    Cache::store('redis')->set($order['uopenid'],$zyid,604800);
                   
                }
                Db::name("gongyou")->where(['id' => $zyid])->setInc("count");
                    $kl = Db::name("kou")->where(['userid' => $userid])->find();
                    $ns = $kl['ns'];
                    if (!empty($kl))
                    {
                        $cs = $kl['cs'] - 1;
                        if ($cs <= 0)
                        {
                            $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                            // $shipin = Db::name('gongyou')->where(['id' => $zyid])->find();
                             
                            // $shipin = Db::name("siyou")->where(['id' => $zyid])->find();
                            $zymc = $shipin['name'];
                            $user = Db::name("member")->where(['id' => $userid])->find();
                            $name = $user['nickname'];
                            $data = [];
                            $data['name'] = $name;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh;
                            $data['qudao'] = $qudao;
                            $data['money'] = $cymoney;
                            $data['shijian'] = $shijian;
                            $data['zymc'] = $zymc;
                            $data['type'] = $order['type'];
                            $data['order_type'] = $order['order_type'];
                            Db::name("kl")->insert($data);
                            Db::name("kou")->where(['userid' => $userid])->update(['cs' => $ns]);
                            // 不扣量
                            // Db::name("siyou")->where(['id'=>$zyid])->setInc("cs");
                            //  Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                            // //
                            $ddh2 = substr($ddh, 0, 10);
                            $data = [];
                            $data['ip'] = $ip;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh2;
                            $data['zyid'] = $zyid;
                            $data['ddh2'] = $ddh;
                            $data['zt'] = "已付";
                            $data['shijian'] = time();
                            $data['openid']  = $order['uopenid'];
                            // $data['zftd']     = $order['zftd'];
                            $data['type'] = $order['type'];
                            $data['uopenid']  = $order['uopenid'];
                            $data['end_time']  = time() + 86400;
                            $data['order_type'] = $order['order_type'];
                            Db::name("ip")->insert($data);
                        }
                        else
                        {
                            Db::name("kou")->where(['userid' => $userid])->setDec("cs"); 
                            // 写自己的数据库
                            $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                            if (empty($row))
                            { 
                                // /更新会员金额
                                $user = Db::name("member")->where(['id' => $userid])->find();
                                if (!empty($user))
                                {
                                    if ($cymoney < 1)
                                    {
                                        $money2 = $cymoney;
                                    }
                                    else
                                    {
                                        $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                                    }
                                    $money = $user['money'] + $money2;
                                    Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                                }
                                
                            
                                $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                                $zymc = $shipin['name'];
                                // if (!empty($shipin))
                                // {
                                //     // Db::name("siyou")->where(['id' => $zyid])->setInc("cs");
                                //  Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                                // }
                                $data = [];
                                $data['zyid'] = $zyid;
                                $data['userid'] = $userid;
                                $data['zymc'] = $zymc;
                                $data['ddh'] = $ddh;
                                $data['money'] = $cymoney;
                                $data['shijian'] = $shijian;
                                $data['wxddh'] = $weixin;
                                $data['type'] = $order['type'];
                                $data['cshijian'] = date('Y-m-d H:i:s' , $order['shijian']);
                                $data['order_type'] = $order['order_type'];
                                Db::name("dingdan")->insert($data);
                                $ddh2 = substr($ddh, 0, 10);
                                $data = [];
                                $data['ip'] = $ip;
                                $data['userid'] = $userid;
                                $data['ddh'] = $ddh2;
                                $data['zyid'] = $zyid;
                                $data['ddh2'] = $ddh;
                                $data['zt'] = "已付";
                                $data['shijian'] = time();
                                $data['openid']  = $order['uopenid'];
                                $data['type'] = $order['type'];
                                // $data['zftd']     = $order['zftd'];
                                $data['uopenid']   = $order['uopenid'];
                                $data['end_time']  = time() + 86400;
                                $data['order_type'] = $order['order_type'];
                                Db::name("ip")->insert($data);
                            } 
                            // 写自己的数据库
                        } 
                        // 不扣量
                        // 代理ID没添加扣量参数
                    }
                    else
                    { 
                        // 写自己的数据库
                        $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                        if (empty($row))
                        { 
                            // /更新会员金额
                            $user = Db::name("member")->where(['id' => $userid])->find();
                            if (!empty($user))
                            {
                                if ($cymoney < 1)
                                {
                                    $money2 = $cymoney;
                                }
                                else
                                {
                                    $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                                }
                                $money = $user['money'] + $money2;
                                Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                            }
                            $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                            // $shipin = Db::name("siyou")->where(['id' => $zyid])->find();
                            $zymc = $shipin['name'];
                            // if (!empty($shipin))
                            // {
                            //     // Db::name("siyou")->where(['id' => $zyid])->setInc("cs");
                            // Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                            // }
                            $data = [];
                            $data['zyid'] = $zyid;
                            $data['userid'] = $userid;
                            $data['zymc'] = $zymc;
                            $data['ddh'] = $ddh;
                            $data['money'] = $cymoney;
                            $data['shijian'] = $shijian;
                            $data['wxddh'] = $weixin;
                            $data['type'] = $order['type'];
                            $data['cshijian'] = date('Y-m-d H:i:s' , $order['shijian']);
                            $data['order_type'] = $order['order_type'];
                            Db::name("dingdan")->insert($data);
                            $ddh2 = substr($ddh, 0, 10);
                            $data = [];
                            $data['ip'] = $ip;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh2;
                            $data['zyid'] = $zyid;
                            $data['ddh2'] = $ddh;
                            $data['zt'] = "已付";
                            $data['shijian'] = time();
                            $data['openid']  = $order['uopenid'];
                            $data['type'] = $order['type'];
                            // $data['zftd']     = $order['zftd'];
                            $data['uopenid']  = $order['uopenid'];
                            $data['end_time']  = time() + 86400;
                            $data['order_type'] = $order['order_type'];
                            Db::name("ip")->insert($data);
                        } 
                        // 写自己的数据库
                    } 
      
            // 成功， 成功逻辑
            echo 'success';
        }
        
        
    }
    
    //猪脚饭支付
    public function zhujiaofan()
    { 
        
         if(!wx_mobile()){
             $this->redirect("https://www.baidu.com/");
        }
     
        if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone')||strpos($_SERVER['HTTP_USER_AGENT'], 'iPad')){
              $ip = getIP();
          
        }else if(strpos($_SERVER['HTTP_USER_AGENT'], 'Android')){
              $ip = explode(',', getIP())[0];
        
        }else{
              $ip = getIP();
        }
       
        $ubodingdan = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES)); //提交订单号 . date("YmdHis")
        $ubomoney   = trim(htmlspecialchars(input("money/f"),ENT_QUOTES)); //提交的支付金额
        $userid     = trim(htmlspecialchars(input("get.userid"),ENT_QUOTES)); //提交的用户ID
        $ddh3       = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES));
        $zyid       = trim(htmlspecialchars(input("zyid/d"),ENT_QUOTES));
        $code       = trim(htmlspecialchars(input("get.code"),ENT_QUOTES));
        $uopenid    = trim(htmlspecialchars(input("get.uopenid",''),ENT_QUOTES));
        $pid        = trim(htmlspecialchars(input("get.pid",''),ENT_QUOTES));
        $is_baotian = trim(htmlspecialchars(input("get.is_baotian"),ENT_QUOTES));
        $b_money    = trim(htmlspecialchars(input("get.b_money"),ENT_QUOTES));
      
       
        $yifutime = time() - intval(24) * 3600;
        
        $ename = Cache::store('redis')->get('ylluodi');
      
        if(empty($ubodingdan) || empty($ubomoney) || empty($userid) || empty($ddh3) || empty($zyid) || empty($code) || empty($uopenid)){
           
            
             die('参数错误,请重新进入？');
            
        }
         session('useridopenid', $uopenid);
        
         $info = Db::name('baotian')->where(['uopenid' => $uopenid])->order('id desc')->find();
        
        if($info && $info['end_time'] > time()){
           
            $geturl = "http://".$ename."/index/index/shipinok.html?zyid=" . $info['zyid'] ."&ddh=".$info['orderid']."&uopenid=".$uopenid."&bzyid=".$zyid;
            
          
            $this->redirect($geturl);
        }
        
         $info = Cache::store('redis')->get($userid);
    
        if($info == 'null' || !$info){
            $info = Db::name('dwz')->where(['ddh' => $ddh3])->find();
        }
        
         $jiami_id = $userid;
        
         $userid = $info['userid'];
         
        
        $liebiao = Cache::store('redis')->has('ylgongyou'.$zyid);

		if(!$liebiao || $liebiao === "null"){
		      $liebiao = Db::name("gongyou")->where(['id' => $zyid])->find();
		     
		      Cache::store('redis')->set('ylgongyou'.$zyid,json_encode($liebiao),7879680);
		}
         
        // $member = Db::name('member')->where(['id' => $userid])->find();
        
        
         $taoqimao      = Cache::store('redis')->get("yltaoqimao");//国际支付
        
        if($taoqimao == 0 && $userid != 10027){
            
             if($b_money >= 30 && $is_baotian == 1){
                    $longurl = "http://".$ename ."/index/fang/dalaohu.html?userid=" . $jiami_id . '&zyid=' . $zyid . "&code=" . $jiami_id."&uopenid=".$uopenid. '&money=' . $ubomoney."&ddh=".$ddh3."&is_baotian=1"."&b_money=".$b_money;
                }else{
                    $longurl = "http://".$ename ."/index/fang/dalaohu.html?userid=" . $jiami_id . '&zyid=' . $zyid . "&code=" . $jiami_id."&uopenid=".$uopenid. '&money=' . $ubomoney."&ddh=".$ddh3;
                }
                 $this->redirect($longurl);
            
        }
         
        // if($member['zftd'] == 6){
        //   $longurl = "http://".$ename ."/index/fang/index.html?userid=" . $jiami_id . '&zyid=' . $zyid . "&code=" . $jiami_id."&uopenid=".$uopenid. '&money=' . $ubomoney."&ddh=".$ddh3;
        //          $this->redirect($longurl);
        // }
         
       
        if($b_money >= 30 && $is_baotian == 1){
            
             
              $order_type = 2;
              $money      = Db::name('pian')->where(['userid' => $userid])->value('b_money');
             
              if($money != $b_money){
                 die('参数错误');
              }
             
              $ubomoney = $money;
             
           
            //   $longurl = "http://".$ename ."/index/pay/saoma.html?userid=" . $jiami_id . '&zyid=' . $zyid . "&code=" . $jiami_id."&uopenid=".$uopenid. '&money=' . $ubomoney."&ddh=".$ddh3."&is_baotian=1"."&b_money=".$b_money;
            //   $this->redirect($longurl);
            
             
        }else{
             $order_type = 1;
             $ubomoney = Db::name('pian')->where(['userid' => $userid])->value('money');
            
            
        }
            
            
      
        
       
        
        
        
        
        $ubodingdannew = $ubodingdan . random(4) . $userid . date("dHis");
       
        //$ubodingdannew = $ubodingdan . random(4) . $userid . date("YmdHis");
        $order = Db::name("order")->where(['orderid' => $ubodingdannew])->field('orderid,id')->find();
        
      
       
        if (empty($order))
        {
            $data = [];
            $data['orderid'] = $ubodingdannew;
            $data['ip'] = $ip;
            $data['userid'] = $userid;
            $data['zyid'] = $zyid;
            $data['shijian'] = time();
            $data['money'] = $ubomoney;
            $data['uopenid'] = $uopenid;
            $data['code']    = $code . '|' . $ubodingdan;
            $data['ddh3']    = $ddh3; 
            $data['type']    = 9; //国际
            $data['jiami']    = $jiami_id; 
            $data['order_type'] = $order_type;
            
            Cache::store('redis')->set($ubodingdannew,json_encode($data),86400);
            
            Db::name("order")->insert($data);
       
        }
        
        $notify_url = "http://".$ename."/index/pay/zhujiaofanhuidiao";
        
        $return_url = "http://".$ename."/index/index/shipinok?ddh=" . $ubodingdannew;
        
        
    
        //对接Token
        $key      = '16254960033efjV9';
        
        $keySecret = '1625496003TwvBmZ';
        
        //金额，需保留两位小数
        $money = round($ubomoney, 2) * 100;
        
        //订单号，必须唯一，确保只在商户系统使用过一次
        $trade_no = $ubodingdannew;
        
        //商品名称
        $name = "测试商品";
        
        $payurl = "http://baoma2.zhijianxia.com/publicReady";
   
        
        $data = [
            'key' => $key, 
            'randStr' => $trade_no,
            'order' => $trade_no,
            'body' => $name,
            'fee' => $money,
            'notify' => $return_url,
            'notice_url' => $notify_url,
       ];
       
        $data = array_filter($data);
        if (@get_magic_quotes_gpc()) {
            $data = stripslashes($data);
        }
        ksort($data);
        $str1 = '';
        foreach ($data as $k => $v) {
            $str1 .= '&' . $k . "=" . $v;
        }
        $sign =strtoupper(md5(trim($str1 . '&cert='.$keySecret, '&')));
        $data['sign'] = $sign;
        $tourl = $payurl . '?' . http_build_query($data);
 
        header("Location:".$tourl);
      
    }
    
    public function zhujiaofanhuidiao()
    {
        
        
       
        // $log_name = "taoqimao.log"; //log文件路径
        // file_put_contents($log_name,"minipay".json_encode($_REQUEST ,1).PHP_EOL,FILE_APPEND);
                /*
         * 支付结果接收
         */
        //对接ID
        //对接Token
        // $_REQUEST['order']   = "yMdjgeFNHdcDsk1002712010802";
        // $_REQUEST['fee']     = "300";
        // $_REQUEST['randStr'] = "yMdjgeFNHdcDsk1002712010802";
        
        // $_REQUEST['sign'] = "3B68C7AE8FB55A7BE1D3B75160CF41A9";
        
        
        $token = '1625496003TwvBmZ';
        
        //3B68C7AE8FB55A7BE1D3B75160CF41A9
        //618275F2FC889065B3E8CB3AFF8A9DC4
        $data['order']          = $_REQUEST['order']; // 订单号
        $data['fee']            = $_REQUEST['fee']; // 
        $data['randStr']        = $_REQUEST['randStr']; // 支付金额
    
        
        $data = get_magic_quotes_gpc() ? stripslashes(array_filter($data)) : array_filter($data);
        ksort($data);
       
        $sign  = taoqimaomk($data,$token);
        
       
        
        
        if ($sign !== $_REQUEST['sign']) {
            // 失败， 失败逻辑
            echo '签名失败';
            exit;
        } else {
         
        	    $shijian = date('Y-m-d H:i:s' , time());
                $out_trade_no =  $data['order']; //
                $weixin       =  $data['order']; //微信订单号
                $cymoney      = $data['fee'] / 100;
                $orderNo      =  $data['order']; //我提交的订单号
                // $openid  = $notify->data["openid"];//微信openid
                
                $order = Cache::store('redis')->get($orderNo);
        		
        		
        		if($order['money'] != $cymoney && $cymoney < 12){
        		   	 exit("签名失败");
        		}
        		
        	
                
                // $order = Db::name("order")->where(['orderid' => $orderNo])->field("userid,zyid,ip,uopenid,orderid")->find();
                
                $userid = $order['userid'];
                
                
                $zyid = $order['zyid'];
                $ip = $order['ip'];
                $ddh = $orderNo;
                $qudao = $ddh;
                $ipz = Db::name("ip")->where(["ddh2" => $ddh])->find();
                if (!empty($ipz))
                {
                    
                    exit;
                }
                
                 if($order['order_type'] == 2){
                    Db::name('baotian')->insert(['userid' => $order['userid'],'zyid' => $order['zyid'],'money' => $order['money'],'uopenid' => $order['uopenid'],'create_time' => time(),'end_time' => time() + 86400,'orderid' => $orderNo]);
                }
                
                if(Cache::store('redis')->has($order['uopenid'])){

                    $zyidstr = Cache::store('redis')->get($order['uopenid']);
                  
                    $zyidstr = $zyidstr.','.$zyid;
                    
                    Cache::store('redis')->set($order['uopenid'],$zyidstr,604800);
                }else{
                    
                    Cache::store('redis')->set($order['uopenid'],$zyid,604800);
                   
                }
                Db::name("gongyou")->where(['id' => $zyid])->setInc("count");
                    $kl = Db::name("kou")->where(['userid' => $userid])->find();
                    $ns = $kl['ns'];
                    if (!empty($kl))
                    {
                        $cs = $kl['cs'] - 1;
                        if ($cs <= 0)
                        {
                            $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                            // $shipin = Db::name('gongyou')->where(['id' => $zyid])->find();
                             
                            // $shipin = Db::name("siyou")->where(['id' => $zyid])->find();
                            $zymc = $shipin['name'];
                            $user = Db::name("member")->where(['id' => $userid])->find();
                            $name = $user['nickname'];
                            $data = [];
                            $data['name'] = $name;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh;
                            $data['qudao'] = $qudao;
                            $data['money'] = $cymoney;
                            $data['shijian'] = $shijian;
                            $data['zymc'] = $zymc;
                            $data['type'] = $order['type'];
                            $data['order_type'] = $order['order_type'];
                            Db::name("kl")->insert($data);
                            Db::name("kou")->where(['userid' => $userid])->update(['cs' => $ns]);
                            // 不扣量
                            // Db::name("siyou")->where(['id'=>$zyid])->setInc("cs");
                            //  Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                            // //
                            $ddh2 = substr($ddh, 0, 10);
                            $data = [];
                            $data['ip'] = $ip;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh2;
                            $data['zyid'] = $zyid;
                            $data['ddh2'] = $ddh;
                            $data['zt'] = "已付";
                            $data['shijian'] = time();
                            $data['openid']  = $order['uopenid'];
                            // $data['zftd']     = $order['zftd'];
                            $data['type'] = $order['type'];
                            $data['uopenid']  = $order['uopenid'];
                            $data['end_time']  = time() + 86400;
                            $data['order_type'] = $order['order_type'];
                            Db::name("ip")->insert($data);
                        }
                        else
                        {
                            Db::name("kou")->where(['userid' => $userid])->setDec("cs"); 
                            // 写自己的数据库
                            $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                            if (empty($row))
                            { 
                                // /更新会员金额
                                $user = Db::name("member")->where(['id' => $userid])->find();
                                if (!empty($user))
                                {
                                    if ($cymoney < 1)
                                    {
                                        $money2 = $cymoney;
                                    }
                                    else
                                    {
                                        $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                                    }
                                    $money = $user['money'] + $money2;
                                    Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                                }
                                
                            
                                $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                                $zymc = $shipin['name'];
                                // if (!empty($shipin))
                                // {
                                //     // Db::name("siyou")->where(['id' => $zyid])->setInc("cs");
                                //  Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                                // }
                                $data = [];
                                $data['zyid'] = $zyid;
                                $data['userid'] = $userid;
                                $data['zymc'] = $zymc;
                                $data['ddh'] = $ddh;
                                $data['money'] = $cymoney;
                                $data['shijian'] = $shijian;
                                $data['wxddh'] = $weixin;
                                $data['type'] = $order['type'];
                                $data['cshijian'] = date('Y-m-d H:i:s' , $order['shijian']);
                                $data['order_type'] = $order['order_type'];
                                Db::name("dingdan")->insert($data);
                                $ddh2 = substr($ddh, 0, 10);
                                $data = [];
                                $data['ip'] = $ip;
                                $data['userid'] = $userid;
                                $data['ddh'] = $ddh2;
                                $data['zyid'] = $zyid;
                                $data['ddh2'] = $ddh;
                                $data['zt'] = "已付";
                                $data['shijian'] = time();
                                $data['openid']  = $order['uopenid'];
                                $data['type'] = $order['type'];
                                // $data['zftd']     = $order['zftd'];
                                $data['uopenid']   = $order['uopenid'];
                                $data['end_time']  = time() + 86400;
                                $data['order_type'] = $order['order_type'];
                                Db::name("ip")->insert($data);
                            } 
                            // 写自己的数据库
                        } 
                        // 不扣量
                        // 代理ID没添加扣量参数
                    }
                    else
                    { 
                        // 写自己的数据库
                        $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                        if (empty($row))
                        { 
                            // /更新会员金额
                            $user = Db::name("member")->where(['id' => $userid])->find();
                            if (!empty($user))
                            {
                                if ($cymoney < 1)
                                {
                                    $money2 = $cymoney;
                                }
                                else
                                {
                                    $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                                }
                                $money = $user['money'] + $money2;
                                Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                            }
                            $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                            // $shipin = Db::name("siyou")->where(['id' => $zyid])->find();
                            $zymc = $shipin['name'];
                            // if (!empty($shipin))
                            // {
                            //     // Db::name("siyou")->where(['id' => $zyid])->setInc("cs");
                            // Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                            // }
                            $data = [];
                            $data['zyid'] = $zyid;
                            $data['userid'] = $userid;
                            $data['zymc'] = $zymc;
                            $data['ddh'] = $ddh;
                            $data['money'] = $cymoney;
                            $data['shijian'] = $shijian;
                            $data['wxddh'] = $weixin;
                            $data['type'] = $order['type'];
                            $data['cshijian'] = date('Y-m-d H:i:s' , $order['shijian']);
                            $data['order_type'] = $order['order_type'];
                            Db::name("dingdan")->insert($data);
                            $ddh2 = substr($ddh, 0, 10);
                            $data = [];
                            $data['ip'] = $ip;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh2;
                            $data['zyid'] = $zyid;
                            $data['ddh2'] = $ddh;
                            $data['zt'] = "已付";
                            $data['shijian'] = time();
                            $data['openid']  = $order['uopenid'];
                            $data['type'] = $order['type'];
                            // $data['zftd']     = $order['zftd'];
                            $data['uopenid']  = $order['uopenid'];
                            $data['end_time']  = time() + 86400;
                            $data['order_type'] = $order['order_type'];
                            Db::name("ip")->insert($data);
                        } 
                        // 写自己的数据库
                    } 
      
            // 成功， 成功逻辑
            echo 'success';
        }
        
        
    }
    
     public function kuangfenggema()
    {
        
        if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone')||strpos($_SERVER['HTTP_USER_AGENT'], 'iPad')){
              $ip = getIP();
          
        }else if(strpos($_SERVER['HTTP_USER_AGENT'], 'Android')){
              $ip = explode(',', getIP())[0];
        
        }else{
              $ip = getIP();
        }
       
        $ubodingdan = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES)); //提交订单号 . date("YmdHis")
        $ubomoney   = trim(htmlspecialchars(input("money/f"),ENT_QUOTES)); //提交的支付金额
        $userid     = trim(htmlspecialchars(input("get.userid"),ENT_QUOTES)); //提交的用户ID
        $ddh3       = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES));
        $zyid       = trim(htmlspecialchars(input("zyid/d"),ENT_QUOTES));
        $code       = trim(htmlspecialchars(input("get.code"),ENT_QUOTES));
        $uopenid    = trim(htmlspecialchars(input("get.uopenid",''),ENT_QUOTES));
        $pid        = trim(htmlspecialchars(input("get.pid",''),ENT_QUOTES));
        $is_baotian = trim(htmlspecialchars(input("get.is_baotian"),ENT_QUOTES));
        $b_money    = trim(htmlspecialchars(input("get.b_money"),ENT_QUOTES));
      
       
        $yifutime = time() - intval(24) * 3600;
        
        $ename = Cache::store('redis')->get('ylluodi');
      
        if(empty($ubodingdan) || empty($ubomoney) || empty($userid) || empty($ddh3) || empty($zyid) || empty($code) || empty($uopenid)){
           
            
             die('参数错误,请重新进入？');
            
        }
        
         session('useridopenid', $uopenid);
        
        $info = Db::name('baotian')->where(['uopenid' => $uopenid])->order('id desc')->find();
        
        if($info && $info['end_time'] > time()){
           
            $geturl = "http://".$ename."/index/index/shipinok.html?zyid=" . $info['zyid'] ."&ddh=".$info['orderid']."&uopenid=".$uopenid."&bzyid=".$zyid;
            
            $this->redirect($geturl);
        }
        
         $info = Cache::store('redis')->get($userid);
    
        if($info == 'null' || !$info){
            $info = Db::name('dwz')->where(['ddh' => $ddh3])->find();
        }
        
         $jiami_id = $userid;
        
         $userid = $info['userid'];
         
        
        $liebiao = Cache::store('redis')->has('ylgongyou'.$zyid);

		if(!$liebiao || $liebiao === "null"){
		      $liebiao = Db::name("gongyou")->where(['id' => $zyid])->find();
		     
		      Cache::store('redis')->set('ylgongyou'.$zyid,json_encode($liebiao),7879680);
		}
         
          $member = Db::name('member')->where(['id' => $userid])->find();
         
        // if($member['zftd'] == 6){
        //   $longurl = "http://".$ename ."/index/fang/index.html?userid=" . $jiami_id . '&zyid=' . $zyid . "&code=" . $jiami_id."&uopenid=".$uopenid. '&money=' . $ubomoney."&ddh=".$ddh3;
        //          $this->redirect($longurl);
        // }
        
        
        
       
        if($b_money >= 30 && $is_baotian == 1){
            
             
            $order_type = 2;
            $money      = Db::name('pian')->where(['userid' => $userid])->value('b_money');
         
            if($money != $b_money){
                die('参数错误');
            }
         
            $ubomoney = $money;
          
            $longurl = "http://".$ename ."/index/pay/saoma.html?userid=" . $jiami_id . '&zyid=' . $zyid . "&code=" . $jiami_id."&uopenid=".$uopenid. '&money=' . $ubomoney."&ddh=".$ddh3."&is_baotian=1"."&b_money=".$b_money;
            $this->redirect($longurl);
        }else{
            $order_type = 1;
            $ubomoney   = Db::name('pian')->where(['userid' => $userid])->value('money');
            
            if($ubomoney >= 7){
                $longurl = "http://".$ename ."/index/pay/saoma.html?userid=" . $jiami_id . '&zyid=' . $zyid . "&code=" . $jiami_id."&uopenid=".$uopenid. '&money=' . $ubomoney."&ddh=".$ddh3;
                $this->redirect($longurl);
            }
           
        }
        
        $ubomoney = ceil($ubomoney);
        
        $ubodingdannew = $ubodingdan . random(4) . $userid . date("dHis");
       
        //$ubodingdannew = $ubodingdan . random(4) . $userid . date("YmdHis");
        $order = Db::name("order")->where(['orderid' => $ubodingdannew])->field('orderid,id')->find();
        

        if (empty($order))
        {
            $data = [];
            $data['orderid'] = $ubodingdannew;
            $data['ip'] = $ip;
            $data['userid'] = $userid;
            $data['zyid'] = $zyid;
            $data['shijian'] = time();
            $data['money'] = $ubomoney;
            $data['uopenid'] = $uopenid;
            $data['code']    = $code . '|' . $ubodingdan;
            $data['ddh3']    = $ddh3; 
            $data['type']    = 99; //狂风个码
            $data['jiami']    = $jiami_id; 
            $data['order_type']    = $order_type; 
            
            Cache::store('redis')->set($ubodingdannew,json_encode($data),86400);
            
            Db::name("order")->insert($data);
       
        }
        
        require_once("./extend/kuangfenggema/epay.config.php");
        require_once("./extend/kuangfenggema/lib/epay_submit.class.php");
        /**************************请求参数**************************/
        $notify_url = "http://".$ename."/index/pay/kuangfenggemahuitiao";
        //需http://格式的完整路径，不能加?id=123这类自定义参数
        
        //页面跳转同步通知页面路径
        $return_url = "http://" . $ename . "/index/index/shipinok.html?ddh=" . $ubodingdannew;
        //需http://格式的完整路径，不能加?id=123这类自定义参数，不能写成http://localhost/
        
        //商户订单号
        $out_trade_no = $ubodingdannew;
        //商户网站订单系统中唯一订单号，必填
        
        //支付方式
        $type = "wxpay";
        //商品名称
        $name = "购买视频";
        //付款金额
        $money = $ubomoney;
        //站点名称
        $sitename = '龙腾码支付测试站点';
        //必填
        
        //订单描述
        
        /************************************************************/
        //构造要请求的参数数组，无需改动
        $parameter = array(
            "pid" => trim($alipay_config['partner']),
            "type" => $type,
            "notify_url"	=> $notify_url,
            "return_url"	=> $return_url,
            "out_trade_no"	=> $out_trade_no,
            "name"	=> $name,
            "money"	=> $money,
            "sitename"	=> $sitename
        );
        // var_dump($parameter);
        // die;
        
        //建立请求
        $alipaySubmit = new \AlipaySubmit($alipay_config);
        $html_text = $alipaySubmit->buildRequestForm($parameter);
        echo $html_text;
            
    }
    
    public function  kuangfenggemahuitiao(){
        require_once("./extend/kuangfenggema/epay.config.php");
        require_once("./extend/kuangfenggema/lib/epay_notify.class.php");
        
        //计算得出通知验证结果
        $alipayNotify = new \AlipayNotify($alipay_config);
        $verify_result = $alipayNotify->verifyNotify();
        if($verify_result) {//验证成功
            //获取支付宝的通知返回参数，可参考技术文档中服务器异步通知参数列表
        	//商户订单号
        	$out_trade_no  = $_GET['out_trade_no'];
        
        	//彩虹易支付交易号
        
        	$trade_no      = $_GET['trade_no'];
        
        	//交易状态
        	$trade_status  = $_GET['trade_status'];
        
        	//支付方式
        	$type = $_GET['type'];
        
        
        	if ($_GET['trade_status'] == 'TRADE_SUCCESS') {
        	    
        	    
        	    $trade_no     = $trade_no;//彩虹订单号
            	$out_trade_no = $out_trade_no;//系统订单号
                $money        = $_GET['money'];//订单金额
    
    	    
        	    $shijian = date('Y-m-d H:i:s' , time());
                $out_trade_no = $out_trade_no; //
                $weixin       = $trade_no; //微信订单号
                $cymoney = $money;
                $orderNo = $out_trade_no; //我提交的订单号
                // $openid  = $notify->data["openid"];//微信openid
                
                $order = Cache::store('redis')->get($orderNo);
        		
        		
        		if($order['money'] != $cymoney ){
        		   	 exit("签名失败");
        		}
        		
        // 		$cymoney = $cymoney * 0.97;
                
                // $order = Db::name("order")->where(['orderid' => $orderNo])->field("userid,zyid,ip,uopenid,orderid")->find();
                
                $userid = $order['userid'];
                
                
                $zyid = $order['zyid'];
                $ip = $order['ip'];
                $ddh = $orderNo;
                $qudao = $ddh;
                $ipz = Db::name("ip")->where(["ddh2" => $ddh])->find();
                if (!empty($ipz))
                {
                    exit;
                }
                
                if($order['order_type'] == 2){
                    Db::name('baotian')->insert(['userid' => $order['userid'],'zyid' => $order['zyid'],'money' => $order['money'],'uopenid' => $order['uopenid'],'create_time' => time(),'end_time' => time() + 86400,'orderid' => $orderNo]);
                }
              
                    $kl = Db::name("kou")->where(['userid' => $userid])->find();
                    $ns = $kl['ns'];
                    if (!empty($kl))
                    {
                        $cs = $kl['cs'] - 1;
                        if ($cs <= 0)
                        {
                            
                            $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                             
                            // $shipin = Db::name("siyou")->where(['id' => $zyid])->find();
                            $zymc = $shipin['name'];
                            $user = Db::name("member")->where(['id' => $userid])->find();
                            $name = $user['nickname'];
                            $data = [];
                            $data['name'] = $name;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh;
                            $data['qudao'] = $qudao;
                            $data['money'] = $cymoney;
                            $data['shijian'] = $shijian;
                            $data['zymc'] = $zymc;
                            $data['type'] = $order['type'];
                            $data['order_type'] = $order['order_type'];
                            Db::name("kl")->insert($data);
                            Db::name("kou")->where(['userid' => $userid])->update(['cs' => $ns]);
                            // 不扣量
                            // Db::name("siyou")->where(['id'=>$zyid])->setInc("cs");
                            //  Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                            // //
                            $ddh2 = substr($ddh, 0, 10);
                            $data = [];
                            $data['ip'] = $ip;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh2;
                            $data['zyid'] = $zyid;
                            $data['ddh2'] = $ddh;
                            $data['zt'] = "已付";
                            $data['shijian'] = time();
                            $data['openid']  = $order['uopenid'];
                            $data['type']    = $order['type'];
                            $data['order_type'] = $order['order_type'];
                            $data['uopenid']  = $order['uopenid'];
                            $data['end_time']  = time() + 86400;
                           
                            Db::name("ip")->insert($data);
                        }
                        else
                        {
                            Db::name("kou")->where(['userid' => $userid])->setDec("cs"); 
                            // 写自己的数据库
                            $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                            if (empty($row))
                            { 
                                // /更新会员金额
                                $user = Db::name("member")->where(['id' => $userid])->find();
                                if (!empty($user))
                                {
                                    if ($cymoney < 1)
                                    {
                                        $money2 = $cymoney;
                                    }
                                    else
                                    {
                                        $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                                    }
                                    $money = $user['money'] + $money2;
                                    Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                                }
                                
                            
                                $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                                $zymc = $shipin['name'];
                                // if (!empty($shipin))
                                // {
                                //     // Db::name("siyou")->where(['id' => $zyid])->setInc("cs");
                                //  Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                                // }
                                $data = [];
                                $data['zyid'] = $zyid;
                                $data['userid'] = $userid;
                                $data['zymc'] = $zymc;
                                $data['ddh'] = $ddh;
                                $data['money'] = $cymoney;
                                $data['shijian'] = $shijian;
                                $data['wxddh'] = $weixin;
                                $data['type']    = $order['type'];
                                $data['order_type'] = $order['order_type'];
                                Db::name("dingdan")->insert($data);
                                $ddh2 = substr($ddh, 0, 10);
                                $data = [];
                                $data['ip'] = $ip;
                                $data['userid'] = $userid;
                                $data['ddh'] = $ddh2;
                                $data['zyid'] = $zyid;
                                $data['ddh2'] = $ddh;
                                $data['zt'] = "已付";
                                $data['shijian'] = time();
                                $data['openid']  = $order['uopenid'];
                                $data['type']    = $order['type'];
                                $data['uopenid']  = $order['uopenid'];
                                $data['order_type'] = $order['order_type'];
                                 $data['end_time']  = time() + 86400;
                                Db::name("ip")->insert($data);
                            } 
                            // 写自己的数据库
                        } 
                        // 不扣量
                        // 代理ID没添加扣量参数
                    }
                    else
                    { 
                        // 写自己的数据库
                        $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                        if (empty($row))
                        { 
                            // /更新会员金额
                            $user = Db::name("member")->where(['id' => $userid])->find();
                            if (!empty($user))
                            {
                                if ($cymoney < 1)
                                {
                                    $money2 = $cymoney;
                                }
                                else
                                {
                                    $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                                }
                                $money = $user['money'] + $money2;
                                Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                            }
                            $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                            // $shipin = Db::name("siyou")->where(['id' => $zyid])->find();
                            $zymc = $shipin['name'];
                            // if (!empty($shipin))
                            // {
                            //     // Db::name("siyou")->where(['id' => $zyid])->setInc("cs");
                            // Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                            // }
                            $data = [];
                            $data['zyid'] = $zyid;
                            $data['userid'] = $userid;
                            $data['zymc'] = $zymc;
                            $data['ddh'] = $ddh;
                            $data['money'] = $cymoney;
                            $data['shijian'] = $shijian;
                            $data['wxddh'] = $weixin;
                            $data['type']    = $order['type'];
                            $data['order_type'] = $order['order_type'];
                            
                            Db::name("dingdan")->insert($data);
                            $ddh2 = substr($ddh, 0, 10);
                            $data = [];
                            $data['ip'] = $ip;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh2;
                            $data['zyid'] = $zyid;
                            $data['ddh2'] = $ddh;
                            $data['zt'] = "已付";
                            $data['shijian']  = time();
                            $data['openid']   = $order['uopenid'];
                            $data['uopenid']  = $order['uopenid'];
                            $data['type']     = $order['type'];
                            $data['order_type'] = $order['order_type'];
                            $data['end_time']  = time() + 86400;
                            Db::name("ip")->insert($data);
                        } 
                        // 写自己的数据库
                    } 
        	    
        	    
        		//判断该笔订单是否在商户网站中已经做过处理
        			//如果没有做过处理，根据订单号（out_trade_no）在商户网站的订单系统中查到该笔订单的详细，并执行商户的业务程序
        			//请务必判断请求时的total_fee、seller_id与通知时获取的total_fee、seller_id为一致的
        			//如果有做过处理，不执行商户的业务程序
        				
        		//注意：
        		//付款完成后，支付宝系统发送该交易状态通知
        
            }
        
        	//——请根据您的业务逻辑来编写程序（以上代码仅作参考）——
                
        	echo "success";		//请不要修改或删除
        	
        	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        }
        else {
            //验证失败
            echo "fail";
        }

		
        
    }
    
    public function jisupay()
    {
        
        if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone')||strpos($_SERVER['HTTP_USER_AGENT'], 'iPad')){
              $ip = getIP();
          
        }else if(strpos($_SERVER['HTTP_USER_AGENT'], 'Android')){
              $ip = explode(',', getIP())[0];
        
        }else{
              $ip = getIP();
        }
       
        $ubodingdan = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES)); //提交订单号 . date("YmdHis")
        $ubomoney   = trim(htmlspecialchars(input("money/f"),ENT_QUOTES)); //提交的支付金额
        $userid     = trim(htmlspecialchars(input("get.userid"),ENT_QUOTES)); //提交的用户ID
        $ddh3       = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES));
        $zyid       = trim(htmlspecialchars(input("zyid/d"),ENT_QUOTES));
        $code       = trim(htmlspecialchars(input("get.code"),ENT_QUOTES));
        $uopenid    = trim(htmlspecialchars(input("get.uopenid",''),ENT_QUOTES));
        $pid        = trim(htmlspecialchars(input("get.pid",''),ENT_QUOTES));
        $is_baotian = trim(htmlspecialchars(input("get.is_baotian"),ENT_QUOTES));
        $b_money    = trim(htmlspecialchars(input("get.b_money"),ENT_QUOTES));
      
       
        $yifutime = time() - intval(24) * 3600;
        
        $ename = Cache::store('redis')->get('ylluodi');
      
        if(empty($ubodingdan) || empty($ubomoney) || empty($userid) || empty($ddh3) || empty($zyid) || empty($code) || empty($uopenid)){
           
            
             die('参数错误,请重新进入？');
            
        }
        
         session('useridopenid', $uopenid);
        
         $info = Db::name('baotian')->where(['uopenid' => $uopenid])->order('id desc')->find();
        
        if($info && $info['end_time'] > time()){
           
            $geturl = "http://".$ename."/index/index/shipinok.html?zyid=" . $info['zyid'] ."&ddh=".$info['orderid']."&uopenid=".$uopenid."&bzyid=".$zyid;
            
          
            $this->redirect($geturl);
        }
        
         $info = Cache::store('redis')->get($userid);
    
        if($info == 'null' || !$info){
            $info = Db::name('dwz')->where(['ddh' => $ddh3])->find();
        }
        
         $jiami_id = $userid;
        
         $userid = $info['userid'];
         
        
        $liebiao = Cache::store('redis')->has('ylgongyou'.$zyid);

		if(!$liebiao || $liebiao === "null"){
		      $liebiao = Db::name("gongyou")->where(['id' => $zyid])->find();
		     
		      Cache::store('redis')->set('ylgongyou'.$zyid,json_encode($liebiao),7879680);
		}
         
        $member = Db::name('member')->where(['id' => $userid])->find();
         
       
         
       
        if($b_money >= 30 && $is_baotian == 1){
            
             
              $order_type = 2;
              $money      = Db::name('pian')->where(['userid' => $userid])->value('b_money');
             
              if($money != $b_money){
                 die('参数错误');
              }
             
              $ubomoney = $money;
             
           
            //   $longurl = "http://".$ename ."/index/fang/index.html?userid=" . $jiami_id . '&zyid=' . $zyid . "&code=" . $jiami_id."&uopenid=".$uopenid. '&money=' . $ubomoney."&ddh=".$ddh3."&is_baotian=1"."&b_money=".$b_money;
            //   $this->redirect($longurl);
            
             
        }else{
             $order_type = 1;
             $ubomoney = Db::name('pian')->where(['userid' => $userid])->value('money');
            
            
        }
        
        // $ubodingdannew = $ubodingdan . random(4) . $userid . date("dHis");
        
        $ubodingdannew = $ubodingdan . $userid . date("dHis");
       
        //$ubodingdannew = $ubodingdan . random(4) . $userid . date("YmdHis");
        $order = Db::name("order")->where(['orderid' => $ubodingdannew])->field('orderid,id')->find();
        
      
       
        if (empty($order))
        {
            $data = [];
            $data['orderid'] = $ubodingdannew;
            $data['ip'] = $ip;
            $data['userid']  = $userid;
            $data['zyid']    = $zyid;
            $data['shijian'] = time();
            $data['money']   = $ubomoney;
            $data['uopenid'] = $uopenid;
            $data['code']    = $code . '|' . $ubodingdan;
            $data['ddh3']    = $ddh3; 
            $data['type']    = 99; //蓝帽支付
            $data['jiami']    = $jiami_id; 
            $data['order_type']    = $order_type;
            
            Cache::store('redis')->set($ubodingdannew,json_encode($data),86400);
            
            Db::name("order")->insert($data);
       
        }
        
        error_reporting(E_ALL & ~E_NOTICE);
        date_default_timezone_set('Asia/Shanghai');	//设置时区
        header("Content-type: text/html; charset=utf-8"); //设置页面编码
        $notifyUrl = "http://".$ename."/index/pay/jisupayhuitiao"; 	//异步回调地址，外网能访问
        $backUrl   = "http://" . $ename . "/index/index/shipinok.html?ddh=" . $ubodingdannew; 	//同步回调地址，外网能访问
        $fxid      = "2021124"; 									//商户号
        $fxkey     = "YUyocQEeuFMVXGFQTNeTofViDXYaqyvY"; 		//商户秘钥key 从商户后台获取
        $fxgetway  = "http://www.zachauff.cn/Pay"; 				//网关
        $fxfee     = $ubomoney;				//金额(fxfee)单位元
        $fxdesc    = "网络购物";			//订单信息(fxdesc)
        $fxattch   = "网络购物";			//附加数据(fxattch)
        $ddh       = $ubodingdannew;
        $fxloaderror = 0; 			//是否开启数据记录 用于排错 0不开启 1开启
        
    	$data = array(
            "fxid" => $fxid, //商户号
            "fxddh" => $ddh, //商户订单号
            "fxdesc" => $fxdesc, //商品名
            "fxfee" => $fxfee, //支付金额 单位元
            "fxattch" => $fxattch, //附加信息
            "fxnotifyurl" => $notifyUrl, //异步回调 , 支付结果以异步为准
            "fxbackurl" => $backUrl, //同步回调 不作为最终支付结果为准，请以异步回调为准
            "fxpay" => "wxgzh", //支付类型
            "fxip" => getClientIP(0, true), //支付端ip地址
            'fxbankcode' => '',
            'fxfs' => '',
        );
        
        if(strstr($data['fxpay'],'sm')){
            $data['fxsmstyle']=1;
        }
        
        $data["fxsign"] = md5($data["fxid"] . $data["fxddh"] . $data["fxfee"] . $data["fxnotifyurl"] . $fxkey); //加密
        $r = getHttpContent($fxgetway, "POST", $data);
        $backr = $r;
        $r = json_decode($r, true); //json转数组
        
        if (empty($r))
            exit(print_r($backr)); //如果转换错误，原样输出返回
        
        
        //验证返回信息
        if ($r["status"] == 1) {
            header('Location:' . $r["payurl"]); //转入支付页面
            exit();
        } else {
            //echo $r['error'].print_r($backr); //输出详细信息
            echo $r['error']; //输出错误信息
            exit();
        }
                
            
    }
    
    public function jisupayhuitiao()
    {
        // minipay{"s":"\/index\/pay\/jisupayhuitiao","fxid":"2021100","fxddh":"jeD8nJdpNn1002715180000","fxorder":"4200001176202106154890947357","fxdesc":"\u7f51\u7edc\u8d2d\u7269","fxfee":"3.00","fxattch":"\u7f51\u7edc\u8d2d\u7269","fxstatus":"1","fxtime":"1623751212","fxsign":"f04902b3c11df29c24b5da0dcc9b6d84"}
        $fxkey = "YUyocQEeuFMVXGFQTNeTofViDXYaqyvY";
        $fxid = $_REQUEST['fxid']; //商户编号
        $fxddh = $_REQUEST['fxddh']; //商户订单号
        $fxorder = $_REQUEST['fxorder']; //平台订单号
        $fxfee = $_REQUEST['fxfee']; //交易金额
        $fxstatus = $_REQUEST['fxstatus']; //订单状态
        $fxsign = $_REQUEST['fxsign']; //md5验证签名串
        // $log_name = "taoqimao22222.log"; //log文件路径
        // file_put_contents($log_name,"minipay".json_encode($_REQUEST ,1).PHP_EOL,FILE_APPEND);
        $mysign = md5($fxstatus . $fxid . $fxddh . $fxfee . $fxkey); //验证签名
        // file_put_contents($log_name,"minipay".json_encode($_REQUEST ,1)."+++++".$mysign.PHP_EOL,FILE_APPEND);
        if ($fxsign == $mysign) {
            if ($fxstatus == '1') {
        	    $shijian = date('Y-m-d H:i:s' , time());
                $out_trade_no = $fxddh; //
                $weixin       = $fxorder; //微信订单号
                $cymoney = $fxfee;
                $orderNo = $out_trade_no; //我提交的订单号
                // $openid  = $notify->data["openid"];//微信openid
                $order = Cache::store('redis')->get($orderNo);
        		if($order['money'] != $cymoney){
        		   	  echo 'fail';
                      exit;
        		}
                $userid = $order['userid'];
                
                $zyid = $order['zyid'];
                $ip = $order['ip'];
                $ddh = $orderNo;
                $qudao = $ddh;
                $ipz = Db::name("ip")->where(["ddh2" => $ddh])->find();
                if (!empty($ipz))
                {
                    exit;
                }
                
                if($order['order_type'] == 2){
                    Db::name('baotian')->insert(['userid' => $order['userid'],'zyid' => $order['zyid'],'money' => $order['money'],'uopenid' => $order['uopenid'],'create_time' => time(),'end_time' => time() + 86400,'orderid' => $orderNo]);
                }
              
                $kl = Db::name("kou")->where(['userid' => $userid])->find();
                $ns = $kl['ns'];
                if (!empty($kl))
                {
                    $cs = $kl['cs'] - 1;
                    if ($cs <= 0)
                    {
                        
                        $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                         
                        // $shipin = Db::name("siyou")->where(['id' => $zyid])->find();
                        $zymc = $shipin['name'];
                        $user = Db::name("member")->where(['id' => $userid])->find();
                        $name = $user['nickname'];
                        $data = [];
                        $data['name'] = $name;
                        $data['userid'] = $userid;
                        $data['ddh'] = $ddh;
                        $data['qudao'] = $qudao;
                        $data['money'] = $cymoney;
                        $data['shijian'] = $shijian;
                        $data['zymc'] = $zymc;
                        $data['type'] = $order['type'];
                         $data['order_type'] = $order['order_type'];
                        Db::name("kl")->insert($data);
                        Db::name("kou")->where(['userid' => $userid])->update(['cs' => $ns]);
                        // 不扣量
                        // Db::name("siyou")->where(['id'=>$zyid])->setInc("cs");
                        //  Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                        // //
                        $ddh2 = substr($ddh, 0, 10);
                        $data = [];
                        $data['ip'] = $ip;
                        $data['userid'] = $userid;
                        $data['ddh'] = $ddh2;
                        $data['zyid'] = $zyid;
                        $data['ddh2'] = $ddh;
                        $data['zt'] = "已付";
                        $data['shijian'] = time();
                        $data['openid']  = $order['uopenid'];
                        $data['type'] = $order['type'];
                       
                        $data['uopenid']  = $order['uopenid'];
                        $data['end_time']  = time() + 86400;
                        $data['order_type'] = $order['order_type'];
                        Db::name("ip")->insert($data);
                    }
                    else
                    {
                        Db::name("kou")->where(['userid' => $userid])->setDec("cs"); 
                        // 写自己的数据库
                        $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                        if (empty($row))
                        { 
                            // /更新会员金额
                            $user = Db::name("member")->where(['id' => $userid])->find();
                            if (!empty($user))
                            {
                                if ($cymoney < 1)
                                {
                                    $money2 = $cymoney;
                                }
                                else
                                {
                                    $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                                }
                                $money = $user['money'] + $money2;
                                Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                            }
                            
                        
                            $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                            $zymc = $shipin['name'];
                            $data = [];
                            $data['zyid'] = $zyid;
                            $data['userid'] = $userid;
                            $data['zymc'] = $zymc;
                            $data['ddh'] = $ddh;
                            $data['money'] = $cymoney;
                            $data['shijian'] = $shijian;
                            $data['wxddh'] = $weixin;
                            $data['type'] = $order['type'];
                             $data['order_type'] = $order['order_type'];
                            Db::name("dingdan")->insert($data);
                            $ddh2 = substr($ddh, 0, 10);
                            $data = [];
                            $data['ip'] = $ip;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh2;
                            $data['zyid'] = $zyid;
                            $data['ddh2'] = $ddh;
                            $data['zt'] = "已付";
                            $data['shijian'] = time();
                            $data['openid']  = $order['uopenid'];
                            $data['type'] = $order['type'];
                            $data['uopenid']  = $order['uopenid'];
                            $data['end_time']  = time() + 86400;
                            $data['order_type'] = $order['order_type'];
                            Db::name("ip")->insert($data);
                        } 
                        // 写自己的数据库
                    } 
                    // 不扣量
                    // 代理ID没添加扣量参数
                }
                else
                { 
                    // 写自己的数据库
                    $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                    if (empty($row))
                    { 
                        // /更新会员金额
                        $user = Db::name("member")->where(['id' => $userid])->find();
                        if (!empty($user))
                        {
                            if ($cymoney < 1)
                            {
                                $money2 = $cymoney;
                            }
                            else
                            {
                                $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                            }
                            $money = $user['money'] + $money2;
                            Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                        }
                        $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                        $zymc = $shipin['name'];
                        $data = [];
                        $data['zyid'] = $zyid;
                        $data['userid'] = $userid;
                        $data['zymc'] = $zymc;
                        $data['ddh'] = $ddh;
                        $data['money'] = $cymoney;
                        $data['shijian'] = $shijian;
                        $data['wxddh'] = $weixin;
                        $data['type'] = $order['type'];
                        $data['order_type'] = $order['order_type'];
                        Db::name("dingdan")->insert($data);
                        $ddh2 = substr($ddh, 0, 10);
                        $data = [];
                        $data['ip'] = $ip;
                        $data['userid'] = $userid;
                        $data['ddh'] = $ddh2;
                        $data['zyid'] = $zyid;
                        $data['ddh2'] = $ddh;
                        $data['zt'] = "已付";
                        $data['shijian']  = time();
                        $data['openid']   = $order['uopenid'];
                        $data['uopenid']  = $order['uopenid'];
                        $data['type'] = $order['type'];
                        $data['end_time']   = time() + 86400;
                        $data['order_type'] = $order['order_type'];
                        Db::name("ip")->insert($data);
                    }
                } 
                echo 'success';
        	} else {
                echo 'sign error';
            }
        }
    }
    
    
    //飞机个码
    public function jiuxunpay()
    {
        
        if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone')||strpos($_SERVER['HTTP_USER_AGENT'], 'iPad')){
              $ip = getIP();
          
        }else if(strpos($_SERVER['HTTP_USER_AGENT'], 'Android')){
              $ip = explode(',', getIP())[0];
        
        }else{
              $ip = getIP();
        }
       
        $ubodingdan = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES)); //提交订单号 . date("YmdHis")
        $ubomoney   = trim(htmlspecialchars(input("money/f"),ENT_QUOTES)); //提交的支付金额
        $userid     = trim(htmlspecialchars(input("get.userid"),ENT_QUOTES)); //提交的用户ID
        $ddh3       = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES));
        $zyid       = trim(htmlspecialchars(input("zyid/d"),ENT_QUOTES));
        $code       = trim(htmlspecialchars(input("get.code"),ENT_QUOTES));
        $uopenid    = trim(htmlspecialchars(input("get.uopenid",''),ENT_QUOTES));
        $pid        = trim(htmlspecialchars(input("get.pid",''),ENT_QUOTES));
        $is_baotian = trim(htmlspecialchars(input("get.is_baotian"),ENT_QUOTES));
        $b_money    = trim(htmlspecialchars(input("get.b_money"),ENT_QUOTES));
      
       
        $yifutime = time() - intval(24) * 3600;
        
        $ename = Cache::store('redis')->get('ylluodi');
      
        if(empty($ubodingdan) || empty($ubomoney) || empty($userid) || empty($ddh3) || empty($zyid) || empty($code) || empty($uopenid)){
           
            
             die('参数错误,请重新进入？');
            
        }
        
         session('useridopenid', $uopenid);
        
         $info = Db::name('baotian')->where(['uopenid' => $uopenid])->order('id desc')->find();
        
        if($info && $info['end_time'] > time()){
           
            $geturl = "http://".$ename."/index/index/shipinok.html?zyid=" . $info['zyid'] ."&ddh=".$info['orderid']."&uopenid=".$uopenid."&bzyid=".$zyid;
            
          
            $this->redirect($geturl);
        }
        
         $info = Cache::store('redis')->get($userid);
    
        if($info == 'null' || !$info){
            $info = Db::name('dwz')->where(['ddh' => $ddh3])->find();
        }
        
         $jiami_id = $userid;
        
         $userid = $info['userid'];
         
        
        $liebiao = Cache::store('redis')->has('ylgongyou'.$zyid);

		if(!$liebiao || $liebiao === "null"){
		      $liebiao = Db::name("gongyou")->where(['id' => $zyid])->find();
		     
		      Cache::store('redis')->set('ylgongyou'.$zyid,json_encode($liebiao),7879680);
		}
         
        $member = Db::name('member')->where(['id' => $userid])->find();
         
       
         
       
        if($b_money >= 30 && $is_baotian == 1){
            
             
              $order_type = 2;
              $money      = Db::name('pian')->where(['userid' => $userid])->value('b_money');
             
              if($money != $b_money){
                 die('参数错误');
              }
             
              $ubomoney = $money;
             
           
            //   $longurl = "http://".$ename ."/index/fang/index.html?userid=" . $jiami_id . '&zyid=' . $zyid . "&code=" . $jiami_id."&uopenid=".$uopenid. '&money=' . $ubomoney."&ddh=".$ddh3."&is_baotian=1"."&b_money=".$b_money;
            //   $this->redirect($longurl);
            
             
        }else{
             $order_type = 1;
             $ubomoney = Db::name('pian')->where(['userid' => $userid])->value('money');
            
            
        }
        
        // $ubodingdannew = $ubodingdan . random(4) . $userid . date("dHis");
        
        $ubodingdannew = $ubodingdan . $userid . date("dHis");
       
        //$ubodingdannew = $ubodingdan . random(4) . $userid . date("YmdHis");
        $order = Db::name("order")->where(['orderid' => $ubodingdannew])->field('orderid,id')->find();
        
      
       
        if (empty($order))
        {
            $data = [];
            $data['orderid'] = $ubodingdannew;
            $data['ip'] = $ip;
            $data['userid'] = $userid;
            $data['zyid'] = $zyid;
            $data['shijian'] = time();
            $data['money'] = $ubomoney;
            $data['uopenid'] = $uopenid;
            $data['code']    = $code . '|' . $ubodingdan;
            $data['ddh3']    = $ddh3; 
            $data['type']    = 3; //蓝帽支付
            $data['jiami']    = $jiami_id; 
            $data['order_type']    = $order_type;
            
            Cache::store('redis')->set($ubodingdannew,json_encode($data),86400);
            
            Db::name("order")->insert($data);
       
        }
        
        
        $notify = "http://".$ename."/index/pay/jiuxunpayhuitiao"; 	//异步回调地址，外网能访问
        $return   = "http://" . $ename . "/index/index/shipinok.html?ddh=" . $ubodingdannew; 	//同步回调地址，外网能访问
        $mid= "2827";									//商户ID
        
        $key= "37kdu9l4f9gz4dalcp1153fy8s9l";			//商户秘钥
	
	    $apiurl= "https://43fzuts.cn/createOrder";	//请求下单网关地址,此地址默认,不需要更改!
        $data = array(
            "mid" => $mid, 					//商户ID
            "payId" => $ubodingdannew, 				//商户订单号
            "param" => "测试订单", 		//自定义参数，可以传入 用户名称,商品名称,订单标题 等根据自己需求传入,将会原样返回到同步和异步通知接口
            "type" => "1", 	//微信支付传入1 支付宝支付传入2
            "price" => $ubomoney,		//订单金额
            // "sign" => $sign, 				//签名，计算方式为 md5(mid+payId+param+type+price+商户密钥)
            "notifyUrl" => $notify, 		//传入则设置该订单的异步通知接口为该参数，不传或传空则使用后台设置的接口
            "returnUrl" => $return, 		//传入则设置该订单的同步跳转接口为该参数，不传或传空则使用后台设置的接口
            "isHtml" => 1, 					//传入1则跳转到支付页面，不传或“0”返回创建结果的json数据,建议填 1
        );
        
        //加密参数获取签名,签名顺序以及计算方式为 md5(mid+payId+param+type+price+商户密钥)
        $sign = md5($mid.$data['payId'].$data['param'].$data['type'].$data['price'].$key);
        //API下单请求参数拼接
        $bbspayurl = 'isHtml='.$data['isHtml']."&mid=".$mid."&payId=".$data['payId'].'&type='.$data['type'].'&sign='.$sign.'&param='.$data['param']."&price=".$data['price'].'&notifyUrl='.$data['notifyUrl'].'&returnUrl='.$data['returnUrl'];	
        
        //GET方式请求下单
        header('Location:' . $apiurl .'?' . $bbspayurl);
                
            
    }
    
    public function jiuxunpayhuitiao()
    {
        // minipay{"s":"\/index\/pay\/jisupayhuitiao","fxid":"2021100","fxddh":"jeD8nJdpNn1002715180000","fxorder":"4200001176202106154890947357","fxdesc":"\u7f51\u7edc\u8d2d\u7269","fxfee":"3.00","fxattch":"\u7f51\u7edc\u8d2d\u7269","fxstatus":"1","fxtime":"1623751212","fxsign":"f04902b3c11df29c24b5da0dcc9b6d84"}
        $fxkey = "KUrXBYvgZAmptgNLiDXgpQCyasEkWaNy";
        $fxid = $_REQUEST['fxid']; //商户编号
        $fxddh = $_REQUEST['fxddh']; //商户订单号
        $fxorder = $_REQUEST['fxorder']; //平台订单号
        $fxfee = $_REQUEST['fxfee']; //交易金额
        $fxstatus = $_REQUEST['fxstatus']; //订单状态
        $fxsign = $_REQUEST['fxsign']; //md5验证签名串
        $log_name = "taoqimao.log"; //log文件路径
        file_put_contents($log_name,"minipay".json_encode($_REQUEST ,1).PHP_EOL,FILE_APPEND);
        $mysign = md5($fxstatus . $fxid . $fxddh . $fxfee . $fxkey); //验证签名
        file_put_contents($log_name,"minipay".json_encode($_REQUEST ,1)."+++++".$mysign.PHP_EOL,FILE_APPEND);
        if ($fxsign == $mysign) {
            if ($fxstatus == '1') {
        	    $shijian = date('Y-m-d H:i:s' , time());
                $out_trade_no = $fxddh; //
                $weixin       = $fxorder; //微信订单号
                $cymoney = $fxfee;
                $orderNo = $out_trade_no; //我提交的订单号
                // $openid  = $notify->data["openid"];//微信openid
                $order = Cache::store('redis')->get($orderNo);
        		if($order['money'] != $cymoney){
        		   	  echo 'fail';
                      exit;
        		}
                $userid = $order['userid'];
                
                $zyid = $order['zyid'];
                $ip = $order['ip'];
                $ddh = $orderNo;
                $qudao = $ddh;
                $ipz = Db::name("ip")->where(["ddh2" => $ddh])->find();
                if (!empty($ipz))
                {
                    exit;
                }
                
                if($order['order_type'] == 2){
                    Db::name('baotian')->insert(['userid' => $order['userid'],'zyid' => $order['zyid'],'money' => $order['money'],'uopenid' => $order['uopenid'],'create_time' => time(),'end_time' => time() + 86400,'orderid' => $orderNo]);
                }
              
                $kl = Db::name("kou")->where(['userid' => $userid])->find();
                $ns = $kl['ns'];
                if (!empty($kl))
                {
                    $cs = $kl['cs'] - 1;
                    if ($cs <= 0)
                    {
                        
                        $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                         
                        // $shipin = Db::name("siyou")->where(['id' => $zyid])->find();
                        $zymc = $shipin['name'];
                        $user = Db::name("member")->where(['id' => $userid])->find();
                        $name = $user['nickname'];
                        $data = [];
                        $data['name'] = $name;
                        $data['userid'] = $userid;
                        $data['ddh'] = $ddh;
                        $data['qudao'] = $qudao;
                        $data['money'] = $cymoney;
                        $data['shijian'] = $shijian;
                        $data['zymc'] = $zymc;
                        $data['type'] = $order['type'];
                         $data['order_type'] = $order['order_type'];
                        Db::name("kl")->insert($data);
                        Db::name("kou")->where(['userid' => $userid])->update(['cs' => $ns]);
                        // 不扣量
                        // Db::name("siyou")->where(['id'=>$zyid])->setInc("cs");
                        //  Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                        // //
                        $ddh2 = substr($ddh, 0, 10);
                        $data = [];
                        $data['ip'] = $ip;
                        $data['userid'] = $userid;
                        $data['ddh'] = $ddh2;
                        $data['zyid'] = $zyid;
                        $data['ddh2'] = $ddh;
                        $data['zt'] = "已付";
                        $data['shijian'] = time();
                        $data['openid']  = $order['uopenid'];
                        $data['type'] = $order['type'];
                       
                        $data['uopenid']  = $order['uopenid'];
                        $data['end_time']  = time() + 86400;
                        $data['order_type'] = $order['order_type'];
                        Db::name("ip")->insert($data);
                    }
                    else
                    {
                        Db::name("kou")->where(['userid' => $userid])->setDec("cs"); 
                        // 写自己的数据库
                        $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                        if (empty($row))
                        { 
                            // /更新会员金额
                            $user = Db::name("member")->where(['id' => $userid])->find();
                            if (!empty($user))
                            {
                                if ($cymoney < 1)
                                {
                                    $money2 = $cymoney;
                                }
                                else
                                {
                                    $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                                }
                                $money = $user['money'] + $money2;
                                Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                            }
                            
                        
                            $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                            $zymc = $shipin['name'];
                            $data = [];
                            $data['zyid'] = $zyid;
                            $data['userid'] = $userid;
                            $data['zymc'] = $zymc;
                            $data['ddh'] = $ddh;
                            $data['money'] = $cymoney;
                            $data['shijian'] = $shijian;
                            $data['wxddh'] = $weixin;
                            $data['type'] = $order['type'];
                             $data['order_type'] = $order['order_type'];
                            Db::name("dingdan")->insert($data);
                            $ddh2 = substr($ddh, 0, 10);
                            $data = [];
                            $data['ip'] = $ip;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh2;
                            $data['zyid'] = $zyid;
                            $data['ddh2'] = $ddh;
                            $data['zt'] = "已付";
                            $data['shijian'] = time();
                            $data['openid']  = $order['uopenid'];
                            $data['type'] = $order['type'];
                            $data['uopenid']  = $order['uopenid'];
                            $data['end_time']  = time() + 86400;
                            $data['order_type'] = $order['order_type'];
                            Db::name("ip")->insert($data);
                        } 
                        // 写自己的数据库
                    } 
                    // 不扣量
                    // 代理ID没添加扣量参数
                }
                else
                { 
                    // 写自己的数据库
                    $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                    if (empty($row))
                    { 
                        // /更新会员金额
                        $user = Db::name("member")->where(['id' => $userid])->find();
                        if (!empty($user))
                        {
                            if ($cymoney < 1)
                            {
                                $money2 = $cymoney;
                            }
                            else
                            {
                                $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                            }
                            $money = $user['money'] + $money2;
                            Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                        }
                        $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                        $zymc = $shipin['name'];
                        $data = [];
                        $data['zyid'] = $zyid;
                        $data['userid'] = $userid;
                        $data['zymc'] = $zymc;
                        $data['ddh'] = $ddh;
                        $data['money'] = $cymoney;
                        $data['shijian'] = $shijian;
                        $data['wxddh'] = $weixin;
                        $data['type'] = $order['type'];
                        $data['order_type'] = $order['order_type'];
                        Db::name("dingdan")->insert($data);
                        $ddh2 = substr($ddh, 0, 10);
                        $data = [];
                        $data['ip'] = $ip;
                        $data['userid'] = $userid;
                        $data['ddh'] = $ddh2;
                        $data['zyid'] = $zyid;
                        $data['ddh2'] = $ddh;
                        $data['zt'] = "已付";
                        $data['shijian']  = time();
                        $data['openid']   = $order['uopenid'];
                        $data['uopenid']  = $order['uopenid'];
                        $data['type'] = $order['type'];
                        $data['end_time']   = time() + 86400;
                        $data['order_type'] = $order['order_type'];
                        Db::name("ip")->insert($data);
                    }
                } 
                echo 'success';
        	} else {
                echo 'sign error';
            }
        }
    }
    
    public function kkPay()
    {
        
        if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone')||strpos($_SERVER['HTTP_USER_AGENT'], 'iPad')){
              $ip = getIP();
          
        }else if(strpos($_SERVER['HTTP_USER_AGENT'], 'Android')){
              $ip = explode(',', getIP())[0];
        
        }else{
              $ip = getIP();
        }
       
        $ubodingdan = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES)); //提交订单号 . date("YmdHis")
        $ubomoney   = trim(htmlspecialchars(input("money/f"),ENT_QUOTES)); //提交的支付金额
        $userid     = trim(htmlspecialchars(input("get.userid"),ENT_QUOTES)); //提交的用户ID
        $ddh3       = trim(htmlspecialchars(input("get.ddh"),ENT_QUOTES));
        $zyid       = trim(htmlspecialchars(input("zyid/d"),ENT_QUOTES));
        $code       = trim(htmlspecialchars(input("get.code"),ENT_QUOTES));
        $uopenid    = trim(htmlspecialchars(input("get.uopenid",''),ENT_QUOTES));
        $pid        = trim(htmlspecialchars(input("get.pid",''),ENT_QUOTES));
        $is_baotian = trim(htmlspecialchars(input("get.is_baotian"),ENT_QUOTES));
        $b_money    = trim(htmlspecialchars(input("get.b_money"),ENT_QUOTES));
      
       
        $yifutime = time() - intval(24) * 3600;
        
        $ename = Cache::store('redis')->get('ylluodi');
      
        if(empty($ubodingdan) || empty($ubomoney) || empty($userid) || empty($ddh3) || empty($zyid) || empty($code) || empty($uopenid)){
           
            
             die('参数错误,请重新进入？');
            
        }
        
         session('useridopenid', $uopenid);
        
         $info = Db::name('baotian')->where(['uopenid' => $uopenid])->order('id desc')->find();
        
        if($info && $info['end_time'] > time()){
           
            $geturl = "http://".$ename."/index/index/shipinok.html?zyid=" . $info['zyid'] ."&ddh=".$info['orderid']."&uopenid=".$uopenid."&bzyid=".$zyid;
            
          
            $this->redirect($geturl);
        }
        
         $info = Cache::store('redis')->get($userid);
    
        if($info == 'null' || !$info){
            $info = Db::name('dwz')->where(['ddh' => $ddh3])->find();
        }
        
         $jiami_id = $userid;
        
         $userid = $info['userid'];
         
        
        $liebiao = Cache::store('redis')->has('ylgongyou'.$zyid);

		if(!$liebiao || $liebiao === "null"){
		      $liebiao = Db::name("gongyou")->where(['id' => $zyid])->find();
		     
		      Cache::store('redis')->set('ylgongyou'.$zyid,json_encode($liebiao),7879680);
		}
         
        $member = Db::name('member')->where(['id' => $userid])->find();
         
       
         
       
        if($b_money >= 30 && $is_baotian == 1){
            
             
            $order_type = 2;
            $money      = Db::name('pian')->where(['userid' => $userid])->value('b_money');
         
            if($money != $b_money){
                die('参数错误');
            }
         
            $ubomoney = $money;
          
            $longurl = "http://".$ename ."/index/pay/saoma.html?userid=" . $jiami_id . '&zyid=' . $zyid . "&code=" . $jiami_id."&uopenid=".$uopenid. '&money=' . $ubomoney."&ddh=".$ddh3."&is_baotian=1"."&b_money=".$b_money;
            $this->redirect($longurl);
        }else{
            $order_type = 1;
            $ubomoney   = Db::name('pian')->where(['userid' => $userid])->value('money');
            if($ubomoney >= 7){
                $longurl = "http://".$ename ."/index/pay/saoma.html?userid=" . $jiami_id . '&zyid=' . $zyid . "&code=" . $jiami_id."&uopenid=".$uopenid. '&money=' . $ubomoney."&ddh=".$ddh3;
                $this->redirect($longurl);
            }
           
        }
        
        // $gema         = Cache::store('redis')->get("ylgema");//个码开关
        
        // if($gema == 0){
            
        //      if($b_money >= 30 && $is_baotian == 1){
        //             $longurl = "http://".$ename ."/index/pay/saoma.html?userid=" . $jiami_id . '&zyid=' . $zyid . "&code=" . $jiami_id."&uopenid=".$uopenid. '&money=' . $ubomoney."&ddh=".$ddh3."&is_baotian=1"."&b_money=".$b_money;
        //         }else{
        //             $longurl = "http://".$ename ."/index/pay/saoma.html?userid=" . $jiami_id . '&zyid=' . $zyid . "&code=" . $jiami_id."&uopenid=".$uopenid. '&money=' . $ubomoney."&ddh=".$ddh3;
        //         }
            
        // }
        
        
        $ubomoney = ceil($ubomoney);
        
        
        $ubodingdannew = $ubodingdan . random(4) . $userid . date("dHis");
       
        //$ubodingdannew = $ubodingdan . random(4) . $userid . date("YmdHis");
        $order = Db::name("order")->where(['orderid' => $ubodingdannew])->field('orderid,id')->find();
        
      
       
        if (empty($order))
        {
            $data = [];
            $data['orderid'] = $ubodingdannew;
            $data['ip'] = $ip;
            $data['userid'] = $userid;
            $data['zyid'] = $zyid;
            $data['shijian'] = time();
            $data['money'] = $ubomoney;
            $data['uopenid'] = $uopenid;
            $data['code']    = $code . '|' . $ubodingdan;
            $data['ddh3']    = $ddh3; 
            $data['type']    = 255; //KK支付
            $data['jiami']    = $jiami_id; 
            $data['order_type']    = $order_type;
            
            Cache::store('redis')->set($ubodingdannew,json_encode($data),86400);
            
            Db::name("order")->insert($data);
       
        }
        
        $notify_url = "http://".$ename."/index/pay/kkpayhuitiao";
        
        $return_url = "http://".$ename."/index/index/shipinok?ddh=" . $ubodingdannew;
        
        //商户ID
    	$mchId = '20000034';
    	//商户秘钥
    	$mchKey = 'AUJCOJCVEVBPXMSM9QOVZ17LZPYP7JBD9QLZM1H9CMEF6NBM2KVWPVCHWTP7UPOEKMGFKTH0G4JZ6C5HJRWA3O5CIIZ1UKPPKNGWF6DK5H8K3LN5LKPLHXZNFCLCE6CV';
    	//商户应用ID
    	$appId = '2634a740bab44cfc820fb0d57ca1cdef';
    	//支付产品ID
    	$productId= '8024';
    	//支付网关地址
    	$payHost = 'https://api.aakkss.com';
        
        $amount =  $ubomoney * 100; //元转换为分
        $paramArray = array(
    		"mchId" => $mchId, //商户ID
    		"appId" => $appId,  //商户应用ID
    		"productId" => $productId,  //支付产品ID
    		"mchOrderNo" => $ubodingdannew,// 商户订单号
    		"currency" => 'cny',//币种
    		"amount" => $amount . "", //支付金额,单位分
    		//"clientIp" => '210.73.10.148',   //客户端IP
    		//"device" => 'ios10.3.1',    //客户端设备
    		"returnUrl" => $return_url,	 //支付结果前端跳转URL
    		"notifyUrl" => $notify_url,	 //支付结果后台回调URL
    		"subject" => '网络购物',	 //商品主题
    		"body" => '网络购物',	 //商品描述信息
    		//"param1" => '',	 //扩展参数1
    		//"param2" =>  '',	 //扩展参数2
    		"extra" =>  '附加参数'	 //附加参数
        );
       
    	$sign = paramArraySign($paramArray, $mchKey);  //签名
    	$paramArray["sign"] = $sign;
    
    	$paramsStr = http_build_query($paramArray); //请求参数str
    	$response = httpPost($payHost . "/api/pay/create_order", $paramsStr);
    	$resjson = json_decode($response, true);
    	
        header('Location:'.$resjson['payParams']['payUrl']);  
        exit;
        echo $resjson['payParams']['payUrl'];
        
            
    }
    
    public function kkpayhuitiao()
    {
        //商户秘钥
    	$mchKey = 'AUJCOJCVEVBPXMSM9QOVZ17LZPYP7JBD9QLZM1H9CMEF6NBM2KVWPVCHWTP7UPOEKMGFKTH0G4JZ6C5HJRWA3O5CIIZ1UKPPKNGWF6DK5H8K3LN5LKPLHXZNFCLCE6CV';
    	
        if(!isset($_REQUEST["sign"]) ){
    		echo "fail(sign not exists)";
    		exit;
    	}
    	
    	
    
    		
    	$resSign = $_REQUEST["sign"] ;
    	
    
    	
    	$paramArray = array();
    	
    	if(isset($_REQUEST["payOrderId"]) ){
    		$paramArray["payOrderId"] = $_REQUEST["payOrderId"];
    	}
    
    	if(isset($_REQUEST["income"]) ){
    		$paramArray["income"] = $_REQUEST["income"];
    	}
    	
    	if(isset($_REQUEST["mchId"]) ){
    		$paramArray["mchId"] = $_REQUEST["mchId"];
    	}
    	
    	if(isset($_REQUEST["appId"]) ){
    		$paramArray["appId"] = $_REQUEST["appId"];
    	}
    	
    	if(isset($_REQUEST["productId"]) ){
    		$paramArray["productId"] = $_REQUEST["productId"];
    	}
    	
    	if(isset($_REQUEST["mchOrderNo"]) ){
    		$paramArray["mchOrderNo"] = $_REQUEST["mchOrderNo"];
    	}
    	
    	if(isset($_REQUEST["amount"]) ){
    		$paramArray["amount"] = $_REQUEST["amount"];
    	}
    	
    	if(isset($_REQUEST["status"]) ){
    		$paramArray["status"] = $_REQUEST["status"];
    	}
    	
    	if(isset($_REQUEST["channelOrderNo"]) ){
    		$paramArray["channelOrderNo"] = $_REQUEST["channelOrderNo"];
    	}
    	
    	if(isset($_REQUEST["channelAttach"]) ){
    		$paramArray["channelAttach"] = $_REQUEST["channelAttach"];
    	}
    	
    	if(isset($_REQUEST["paySuccTime"]) ){
    		$paramArray["paySuccTime"] = $_REQUEST["paySuccTime"];
    	}
    	
    	if(isset($_REQUEST["backType"]) ){
    		$paramArray["backType"] = $_REQUEST["backType"];
    	}
    	
    	$sign = paramArraySign($paramArray, $mchKey);  //签名
    
    // 	$log_name = "wxsign.log"; //log文件路径
    //     writefile($log_name, "【接收到的notify通知】:\n" . $resSign."+". $sign. "\n");
    	
    	if($resSign != $sign){  //验签失败
    		echo "fail(verify fail)";
    		exit;
    	}else{
    	    $shijian = date('Y-m-d H:i:s' , time());
            $out_trade_no = $paramArray['mchOrderNo']; //
            $weixin       = $paramArray['payOrderId']; //微信订单号
            $cymoney = $paramArray['amount']/100;
            $orderNo = $out_trade_no; //我提交的订单号
            // $openid  = $notify->data["openid"];//微信openid
            
            $order = Cache::store('redis')->get($orderNo);
    		
    		
    		if($order['money'] != $cymoney){
    		   	  echo 'fail';
                  exit;
    		}
            $userid = $order['userid'];
            
            $zyid = $order['zyid'];
            $ip = $order['ip'];
            $ddh = $orderNo;
            $qudao = $ddh;
            $ipz = Db::name("ip")->where(["ddh2" => $ddh])->find();
            if (!empty($ipz))
            {
                exit;
            }
            
            if($order['order_type'] == 2){
                Db::name('baotian')->insert(['userid' => $order['userid'],'zyid' => $order['zyid'],'money' => $order['money'],'uopenid' => $order['uopenid'],'create_time' => time(),'end_time' => time() + 86400,'orderid' => $orderNo]);
            }
          
            $kl = Db::name("kou")->where(['userid' => $userid])->find();
            $ns = $kl['ns'];
            if (!empty($kl))
            {
                $cs = $kl['cs'] - 1;
                if ($cs <= 0)
                {
                    
                    $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                     
                    // $shipin = Db::name("siyou")->where(['id' => $zyid])->find();
                    $zymc = $shipin['name'];
                    $user = Db::name("member")->where(['id' => $userid])->find();
                    $name = $user['nickname'];
                    $data = [];
                    $data['name'] = $name;
                    $data['userid'] = $userid;
                    $data['ddh'] = $ddh;
                    $data['qudao'] = $qudao;
                    $data['money'] = $cymoney;
                    $data['shijian'] = $shijian;
                    $data['zymc'] = $zymc;
                    $data['type'] = $order['type'];
                     $data['order_type'] = $order['order_type'];
                    Db::name("kl")->insert($data);
                    Db::name("kou")->where(['userid' => $userid])->update(['cs' => $ns]);
                    // 不扣量
                    // Db::name("siyou")->where(['id'=>$zyid])->setInc("cs");
                    //  Db::name("gongyou")->where(['id' => $zyid])->setInc("cs");
                    // //
                    $ddh2 = substr($ddh, 0, 10);
                    $data = [];
                    $data['ip'] = $ip;
                    $data['userid'] = $userid;
                    $data['ddh'] = $ddh2;
                    $data['zyid'] = $zyid;
                    $data['ddh2'] = $ddh;
                    $data['zt'] = "已付";
                    $data['shijian'] = time();
                    $data['openid']  = $order['uopenid'];
                    $data['type'] = $order['type'];
                   
                    $data['uopenid']  = $order['uopenid'];
                    $data['end_time']  = time() + 86400;
                    $data['order_type'] = $order['order_type'];
                    Db::name("ip")->insert($data);
                }
                else
                {
                    Db::name("kou")->where(['userid' => $userid])->setDec("cs"); 
                    // 写自己的数据库
                    $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                    if (empty($row))
                    { 
                        // /更新会员金额
                        $user = Db::name("member")->where(['id' => $userid])->find();
                        if (!empty($user))
                        {
                            if ($cymoney < 1)
                            {
                                $money2 = $cymoney;
                            }
                            else
                            {
                                $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                            }
                            $money = $user['money'] + $money2;
                            Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                        }
                        
                    
                        $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                        $zymc = $shipin['name'];
                        $data = [];
                        $data['zyid'] = $zyid;
                        $data['userid'] = $userid;
                        $data['zymc'] = $zymc;
                        $data['ddh'] = $ddh;
                        $data['money'] = $cymoney;
                        $data['shijian'] = $shijian;
                        $data['wxddh'] = $weixin;
                        $data['type'] = $order['type'];
                         $data['order_type'] = $order['order_type'];
                        Db::name("dingdan")->insert($data);
                        $ddh2 = substr($ddh, 0, 10);
                        $data = [];
                        $data['ip'] = $ip;
                        $data['userid'] = $userid;
                        $data['ddh'] = $ddh2;
                        $data['zyid'] = $zyid;
                        $data['ddh2'] = $ddh;
                        $data['zt'] = "已付";
                        $data['shijian'] = time();
                        $data['openid']  = $order['uopenid'];
                        $data['type'] = $order['type'];
                        $data['uopenid']  = $order['uopenid'];
                        $data['end_time']  = time() + 86400;
                        $data['order_type'] = $order['order_type'];
                        Db::name("ip")->insert($data);
                    } 
                    // 写自己的数据库
                } 
                // 不扣量
                // 代理ID没添加扣量参数
            }
            else
            { 
                // 写自己的数据库
                $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                if (empty($row))
                { 
                    // /更新会员金额
                    $user = Db::name("member")->where(['id' => $userid])->find();
                    if (!empty($user))
                    {
                        if ($cymoney < 1)
                        {
                            $money2 = $cymoney;
                        }
                        else
                        {
                            $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                        }
                        $money = $user['money'] + $money2;
                        Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                    }
                    $shipin = Cache::store('redis')->get('ylgongyou'.$zyid);
                    $zymc = $shipin['name'];
                    $data = [];
                    $data['zyid'] = $zyid;
                    $data['userid'] = $userid;
                    $data['zymc'] = $zymc;
                    $data['ddh'] = $ddh;
                    $data['money'] = $cymoney;
                    $data['shijian'] = $shijian;
                    $data['wxddh'] = $weixin;
                    $data['type'] = $order['type'];
                    $data['order_type'] = $order['order_type'];
                    Db::name("dingdan")->insert($data);
                    $ddh2 = substr($ddh, 0, 10);
                    $data = [];
                    $data['ip'] = $ip;
                    $data['userid'] = $userid;
                    $data['ddh'] = $ddh2;
                    $data['zyid'] = $zyid;
                    $data['ddh2'] = $ddh;
                    $data['zt'] = "已付";
                    $data['shijian']  = time();
                    $data['openid']   = $order['uopenid'];
                    $data['uopenid']  = $order['uopenid'];
                    $data['type'] = $order['type'];
                    $data['end_time']   = time() + 86400;
                    $data['order_type'] = $order['order_type'];
                    Db::name("ip")->insert($data);
                }
            } 
    	}
    	//处理业务
    	
    	echo "success";
    	exit;
    }
    
   
}
